pa_stream_begin_write fail, PA_ERR_BADSTATE
Summary
Using pulse audio to help with transmitting audio across 2 clients. The linux client cannot hear the windows one, but the windows one can hear the linux one. When debugging pa_stream_begin_write will fail with error code PA_ERR_BADSTATE.
environment
using Visual Stuido, WSL2, Ubuntu 20 to build Linux library using Pulse Audio
code
//Check if Pulse Audio is initialized.
//get audio and create new platform stream
SourceVoice* sourceVoice = FindExistingSourceVoice(identifierContext);
if (sourceVoice == nullptr)
{
for (SourceVoice& sourceVoiceCandidate : m_sourceVoices)
{
if (sourceVoiceCandidate.identifierContext == nullptr)
{
auto platformStream = pa_stream_new
... \\checks to make sure stream is created
int error = pa_stream_connect_playback
... \\checks to make sure connection succeeded
}
}
PulseAudioLocker lock(m_pulseAudioMainLoop);
// Update the volume for the stream input if it has changed.
SetPulseAudioVolumeForSourceVoice(sourceVoice, volume, m_pulseAudioContext, m_pulseAudioSampleSpec.channels);
size_t writeableSize = pa_stream_writable_size(sourceVoice->pulseAudioStream);
void* streamBuffer = nullptr;
size_t streamBufferSize = (size_t)-1;
int error = pa_stream_begin_write(
sourceVoice->pulseAudioStream,
&streamBuffer,
&streamBufferSize);
if (error != 0)
{
pa_stream_cancel_write(sourceVoice->pulseAudioStream);
DbgLogErr("PulseAudio stream begin write failed! (error %i, errorMessage %s)", error, pa_strerror(error));
RETURN_VOID();
}
What is the current bug behavior?
Error Bad State is returned
What is the expected correct behavior?
No error should happen
What I tired?
I tried making a while loop and wait for the state to change to then continue but the state doesn't seem to change the loop was placed before locking the variables.