From 2e3b67ba1b6629fa11f513ec6e974079484fcd9d Mon Sep 17 00:00:00 2001 From: Tuukka Pasanen Date: Fri, 14 Feb 2025 10:08:37 +0200 Subject: [PATCH 1/2] pulseaudio: Correct output writing place where it's copied Output copying what in incorrect place now that should be corrected --- .../pulseaudio/pa_linux_pulseaudio_cb.c | 79 +++++++++++++++---- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c b/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c index 4c86c7d57..4890b06c0 100644 --- a/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c +++ b/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c @@ -309,8 +309,20 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, if( !stream->isActive && stream->pulseaudioIsActive && stream->outputStream) { - bufferData = pulseaudioSampleBuffer; - memset( bufferData, 0x00, length); + size_t tmpSize = length; + + /* 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) + { + return paNotInitialized; + } + + memset( bufferData, 0x00, tmpSize); pa_stream_write( stream->outputStream, bufferData, @@ -323,8 +335,16 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, } - while(1) + do { + /* Make sure that bufferData is NULL that marks there + * is nothing to output + */ + if( isOutputCb ) + { + bufferData = NULL; + } + /* * If everything fail like stream vanish or mainloop * is in error state try to handle error @@ -396,6 +416,9 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, hostFramesPerBuffer ); } + /* Is output is enable and buffer data is not NULL + * then crap pointer to output ringbuffer + */ if( isOutputCb ) { @@ -404,14 +427,6 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, /* 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) - { - return paNotInitialized; - } - PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, 0, bufferData, @@ -420,6 +435,31 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, PaUtil_SetOutputFrameCount( &stream->bufferProcessor, hostFramesPerBuffer ); + } + + hostFrameCount = + PaUtil_EndBufferProcessing( &stream->bufferProcessor, + &ret ); + + PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, + hostFrameCount ); + + /* + * pa_stream_begin_write gives pointer to ring + * buffer in Pulseaudio. WhenPaUtil_EndBufferProcessing + * returns paContinue then is ok to write output. + * + * When it's something else than paContinue + * then we have to cancel writing with + * with pa_stream_cancel_write. + * + * If there is no space in output ringbuffer or for + * example USB device has dissapiered and pointer + * is NULL then we just get out of loop as there is not much + * we can't do + */ + if( ret == paContinue && isOutputCb && bufferData ) + { if( pa_stream_write( stream->outputStream, bufferData, pulseaudioOutputBytes, @@ -433,14 +473,19 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, pulseaudioOutputWritten += pulseaudioOutputBytes; } + else if( ret != paContinue && isOutputCb && bufferData ) + { + pa_stream_cancel_write( stream->outputStream ); + bufferData = NULL; + } + else if( isOutputCb && !bufferData ) + { + ret = -1; + } + } + while( ret == paContinue ); - hostFrameCount = - PaUtil_EndBufferProcessing( &stream->bufferProcessor, - &ret ); - PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, - hostFrameCount ); - } return ret; } From 87f1f339aa3d77600fcd762a40a9c8e3ab8e0149 Mon Sep 17 00:00:00 2001 From: Ross Bencina Date: Fri, 13 Jun 2025 08:38:47 +1000 Subject: [PATCH 2/2] Fix indent on multi-line statement (use double indent) --- src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c b/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c index 4890b06c0..b1cdc8c46 100644 --- a/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c +++ b/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c @@ -438,8 +438,8 @@ static int _PaPulseAudio_ProcessAudio(PaPulseAudio_Stream *stream, } hostFrameCount = - PaUtil_EndBufferProcessing( &stream->bufferProcessor, - &ret ); + PaUtil_EndBufferProcessing( &stream->bufferProcessor, + &ret ); PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, hostFrameCount );