Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix occasional audio interface deadlock #4450

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/AudioJack.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <QtCore/QVector>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QMutexLocker>

#include "AudioDevice.h"
#include "AudioDeviceSetupWidget.h"
Expand Down Expand Up @@ -108,7 +107,6 @@ private slots:

bool m_active;
bool m_stopped;
QMutex m_processingMutex;

MidiJack *m_midiClient;
QVector<jack_port_t *> m_outputPorts;
Expand Down
1 change: 0 additions & 1 deletion include/AudioPortAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class AudioPortAudio : public AudioDevice
int m_outBufSize;

bool m_stopped;
QSemaphore m_stopSemaphore;

} ;

Expand Down
2 changes: 2 additions & 0 deletions include/AudioSoundIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class AudioSoundIo : public AudioDevice
fpp_t m_outBufFramesTotal;
fpp_t m_outBufFrameIndex;

bool m_stopped;

int m_disconnectErr;
void onBackendDisconnect(int err);

Expand Down
6 changes: 6 additions & 0 deletions include/fifo_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class fifoBuffer
return( element );
}

void waitUntilRead()
{
m_writer_sem.acquire( m_size );
m_writer_sem.release( m_size );
}

bool available()
{
return( m_reader_sem.available() );
Expand Down
6 changes: 4 additions & 2 deletions src/core/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,17 @@ void Mixer::storeAudioDevice()

void Mixer::restoreAudioDevice()
{
if( m_oldAudioDev != NULL )
if( m_oldAudioDev && m_audioDev != m_oldAudioDev )
{
stopProcessing();
delete m_audioDev;

m_audioDev = m_oldAudioDev;
emit sampleRateChanged();

m_oldAudioDev = NULL;
startProcessing();
}
m_oldAudioDev = NULL;
}


Expand Down Expand Up @@ -1127,7 +1127,9 @@ void Mixer::fifoWriter::run()
write( buffer );
}

// Let audio backend stop processing
write( NULL );
m_fifo->waitUntilRead();
}


Expand Down
4 changes: 1 addition & 3 deletions src/core/audio/AudioJack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ bool AudioJack::initJackClient()

void AudioJack::startProcessing()
{
QMutexLocker m( &m_processingMutex );
m_stopped = false;

if( m_active || m_client == NULL )
Expand Down Expand Up @@ -254,7 +253,6 @@ void AudioJack::startProcessing()

void AudioJack::stopProcessing()
{
QMutexLocker m( &m_processingMutex );
m_stopped = true;
}

Expand Down Expand Up @@ -342,7 +340,6 @@ void AudioJack::renamePort( AudioPort * _port )

int AudioJack::processCallback( jack_nframes_t _nframes, void * _udata )
{
QMutexLocker m( &m_processingMutex );

// do midi processing first so that midi input can
// add to the following sound processing
Expand Down Expand Up @@ -406,6 +403,7 @@ int AudioJack::processCallback( jack_nframes_t _nframes, void * _udata )
m_framesDoneInCurBuf = 0;
if( !m_framesToDoInCurBuf )
{
m_stopped = true;
break;
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/core/audio/AudioPortAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ AudioPortAudio::AudioPortAudio( bool & _success_ful, Mixer * _mixer ) :
m_paStream( NULL ),
m_wasPAInitError( false ),
m_outBuf( new surroundSampleFrame[mixer()->framesPerPeriod()] ),
m_outBufPos( 0 ),
m_stopSemaphore( 1 )
m_outBufPos( 0 )
{
_success_ful = false;

Expand Down Expand Up @@ -167,8 +166,6 @@ AudioPortAudio::AudioPortAudio( bool & _success_ful, Mixer * _mixer ) :
printf( "Input device: '%s' backend: '%s'\n", Pa_GetDeviceInfo( inDevIdx )->name, Pa_GetHostApiInfo( Pa_GetDeviceInfo( inDevIdx )->hostApi )->name );
printf( "Output device: '%s' backend: '%s'\n", Pa_GetDeviceInfo( outDevIdx )->name, Pa_GetHostApiInfo( Pa_GetDeviceInfo( outDevIdx )->hostApi )->name );

m_stopSemaphore.acquire();

// TODO: debug Mixer::pushInputFrames()
//m_supportsCapture = true;

Expand All @@ -181,7 +178,6 @@ AudioPortAudio::AudioPortAudio( bool & _success_ful, Mixer * _mixer ) :
AudioPortAudio::~AudioPortAudio()
{
stopProcessing();
m_stopSemaphore.release();

if( !m_wasPAInitError )
{
Expand Down Expand Up @@ -212,8 +208,7 @@ void AudioPortAudio::stopProcessing()
{
if( m_paStream && Pa_IsStreamActive( m_paStream ) )
{
m_stopSemaphore.acquire();

m_stopped = true;
PaError err = Pa_StopStream( m_paStream );

if( err != paNoError )
Expand Down Expand Up @@ -283,7 +278,6 @@ int AudioPortAudio::process_callback(
if( !frames )
{
m_stopped = true;
m_stopSemaphore.release();
memset( _outputBuffer, 0, _framesPerBuffer *
channels() * sizeof(float) );
return paComplete;
Expand Down
1 change: 1 addition & 0 deletions src/core/audio/AudioPulseAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void AudioPulseAudio::startProcessing()

void AudioPulseAudio::stopProcessing()
{
m_quit = true;
stopProcessingThread( this );
}

Expand Down
1 change: 1 addition & 0 deletions src/core/audio/AudioSdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ void AudioSdl::sdlAudioCallback( Uint8 * _buf, int _len )
const fpp_t frames = getNextBuffer( m_outBuf );
if( !frames )
{
m_stopped = true;
memset( _buf, 0, _len );
return;
}
Expand Down
21 changes: 21 additions & 0 deletions src/core/audio/AudioSoundIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ AudioSoundIo::AudioSoundIo( bool & outSuccessful, Mixer * _mixer ) :
m_disconnectErr = 0;
m_outBufFrameIndex = 0;
m_outBufFramesTotal = 0;
m_stopped = true;

m_soundio = soundio_create();
if (!m_soundio)
Expand Down Expand Up @@ -210,15 +211,18 @@ void AudioSoundIo::startProcessing()

m_outBuf = new surroundSampleFrame[m_outBufSize];

m_stopped = false;
int err;
if ((err = soundio_outstream_start(m_outstream)))
{
m_stopped = true;
fprintf(stderr, "soundio unable to start stream: %s\n", soundio_strerror(err));
}
}

void AudioSoundIo::stopProcessing()
{
m_stopped = true;
if (m_outstream)
{
soundio_outstream_destroy(m_outstream);
Expand All @@ -244,6 +248,7 @@ void AudioSoundIo::underflowCallback()

void AudioSoundIo::writeCallback(int frameCountMin, int frameCountMax)
{
if (m_stopped) {return;}
const struct SoundIoChannelLayout *layout = &m_outstream->layout;
SoundIoChannelArea *areas;
int bytesPerSample = m_outstream->bytes_per_sample;
Expand All @@ -265,11 +270,27 @@ void AudioSoundIo::writeCallback(int frameCountMin, int frameCountMax)
if (!frameCount)
break;


if (m_stopped)
{
for (int channel = 0; channel < layout->channel_count; ++channel)
{
memset(areas[channel].ptr, 0, bytesPerSample * frameCount);
areas[channel].ptr += areas[channel].step * frameCount;
}
continue;
}

for (int frame = 0; frame < frameCount; frame += 1)
{
if (m_outBufFrameIndex >= m_outBufFramesTotal)
{
m_outBufFramesTotal = getNextBuffer(m_outBuf);
if (m_outBufFramesTotal == 0)
{
m_stopped = true;
break;
}
m_outBufFrameIndex = 0;
}

Expand Down