Skip to content

Commit

Permalink
JACK: try to use stable port names (#888)
Browse files Browse the repository at this point in the history
JACK connection managers save/load the connection graph so users don't
need to create connections manually every time. This depends on stable
port names but PortAudio currently generates incrementing port names
throughout the lifetime of a running process.

Reset the port name counters when all streams are closed so that the
same port names are generated when the streams are opened again.

This is not a perfect solution because applications that open streams in
different orders each time or never close all open streams before
restarting audio processing will not reset the port counters. However,
typical applications should work with this simple approach and it's an
improvement over current behavior.

Tested with Audacity 3.3.3 on Fedora 39.

Signed-off-by: Stefan Hajnoczi <[email protected]>
Co-authored-by: Stefan Hajnoczi <[email protected]>
  • Loading branch information
stefanha and Stefan Hajnoczi authored Apr 19, 2024
1 parent 7171f07 commit db778cf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/hostapi/jack/pa_jack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,26 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
return result;
}

/*
* Reset inputBase and outputBase when all streams have been closed. This makes port names stable for typical
* applications that open streams in the same order every time. Applications that open streams in a different order
* every time do not get stable port names. Stable port names allow JACK connection managers to save/load connections
* between ports, saving the user the trouble of manually connecting ports each time they use the application.
*/
static void CheckAndResetPortBase( PaJackHostApiRepresentation *jackApi )
{
int noStreams;

ASSERT_CALL( pthread_mutex_lock( &jackApi->mtx ), 0 );
noStreams = jackApi->jackIsDown || jackApi->processQueue == NULL;
ASSERT_CALL( pthread_mutex_unlock( &jackApi->mtx ), 0 );

if ( noStreams ) {
jackApi->inputBase = 0;
jackApi->outputBase = 0;
}
}

/*
When CloseStream() is called, the multi-api layer ensures that
the stream has already been stopped or aborted.
Expand All @@ -1397,12 +1417,14 @@ static PaError CloseStream( PaStream* s )
{
PaError result = paNoError;
PaJackStream *stream = (PaJackStream*)s;
PaJackHostApiRepresentation *hostApi = stream->hostApi;

/* Remove this stream from the processing queue */
ENSURE_PA( RemoveStream( stream ) );

error:
CleanUpStream( stream, 1, 1 );
CheckAndResetPortBase( hostApi );
return result;
}

Expand Down

0 comments on commit db778cf

Please sign in to comment.