Skip to content

Commit

Permalink
pulseaudio: Rework stopping code that it should prevent artifacts
Browse files Browse the repository at this point in the history
Rework stopping and stream closing code as it should prevent artifacts
when closing or stopping stream. Correctly handle stream termination
from callback
  • Loading branch information
illuusio committed Oct 4, 2024
1 parent 57aa393 commit d47968e
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream,
int ret = paContinue;
void *bufferData = NULL;
size_t pulseaudioOutputWritten = 0;
size_t pulseaudioLength = length;

/* If there is no specified per host buffer then
* just generate one or but correct one in place
Expand Down Expand Up @@ -293,19 +294,29 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream,
pulseaudioInputBytes /= 2;
}

if( !stream->isActive && stream->pulseaudioIsActive && stream->outputStream)
if( !stream->isActive && stream->outputStream)
{
bufferData = pulseaudioSampleBuffer;
memset( bufferData, 0x00, length);
outputzero:
size_t tmpSize = pulseaudioLength;

/* Allocate memory to make it faster to output stuff */
if( pa_stream_begin_write( stream->outputStream, &bufferData, &tmpSize ) )
{
PA_DEBUG( ("Portaudio %s: Can't output to stream!\n",
__FUNCTION__) );
return paInsufficientMemory;
}

memset( bufferData, 0x00, tmpSize);

pa_stream_write( stream->outputStream,
bufferData,
length,
tmpSize,
NULL,
0,
PA_SEEK_RELATIVE );

return paContinue;
return ret;
}


Expand Down Expand Up @@ -388,13 +399,10 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream,
size_t tmpSize = pulseaudioOutputBytes;

/* Allocate memory to make it faster to output stuff */
pa_stream_begin_write( stream->outputStream, &bufferData, &tmpSize );

/* If bufferData is NULL then output is not ready
* and we have to wait for it
*/
if(!bufferData)
if( pa_stream_begin_write( stream->outputStream, &bufferData, &tmpSize ) )
{
PA_DEBUG( ("Portaudio %s: Can't output to stream!\n",
__FUNCTION__) )
return paNotInitialized;
}

Expand Down Expand Up @@ -426,6 +434,13 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream,

PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer,
hostFrameCount );

if( ret )
{
stream->isActive = 0;
pulseaudioLength = length - pulseaudioOutputWritten;
goto outputzero;
}
}

return ret;
Expand Down Expand Up @@ -518,8 +533,6 @@ PaError PaPulseAudio_CloseStreamCb( PaStream * s )
/* Wait for stream to be stopped */
stream->isActive = 0;
stream->isStopped = 1;
stream->pulseaudioIsActive = 0;
stream->pulseaudioIsStopped = 1;

if( stream->outputStream != NULL
&& PA_STREAM_IS_GOOD( pa_stream_get_state( stream->outputStream ) ) )
Expand All @@ -536,7 +549,6 @@ PaError PaPulseAudio_CloseStreamCb( PaStream * s )
&pulseaudioOperation );

PaPulseAudio_Lock(stream->mainloop);

pa_stream_disconnect( stream->outputStream );
PaPulseAudio_UnLock( stream->mainloop );
}
Expand Down Expand Up @@ -598,6 +610,9 @@ PaError PaPulseAudio_CloseStreamCb( PaStream * s )
usleep(10000);
}

stream->pulseaudioIsActive = 0;
stream->pulseaudioIsStopped = 1;

PaUtil_TerminateBufferProcessor( &stream->bufferProcessor );
PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation );

Expand Down Expand Up @@ -917,8 +932,6 @@ static PaError RequestStop( PaPulseAudio_Stream * stream,
/* Wait for stream to be stopped */
stream->isActive = 0;
stream->isStopped = 1;
stream->pulseaudioIsActive = 0;
stream->pulseaudioIsStopped = 1;

stream->missedBytes = 0;

Expand All @@ -941,8 +954,6 @@ static PaError RequestStop( PaPulseAudio_Stream * stream,

requeststop_error:
PaPulseAudio_UnLock( pulseaudioHostApi->mainloop );
stream->isActive = 0;
stream->isStopped = 1;
stream->pulseaudioIsActive = 0;
stream->pulseaudioIsStopped = 1;

Expand Down

0 comments on commit d47968e

Please sign in to comment.