Open RJVB opened 2 months ago
This patch seems to work; dependent applications will get the choice of the audio devices accessible through those of the configured APIs that initialised correctly.
For instance, on Linux, applications using this patched portaudio can provide or record sound even if the pulseaudio daemon was (temporarily) killed.
diff --git src/common/pa_front.c src/common/pa_front.c
index 9f81f26..aad1917 100644
--- src/common/pa_front.c
+++ src/common/pa_front.c
@@ -222,47 +222,58 @@ static PaError InitializeHostApis( void )
PA_DEBUG(( "before paHostApiInitializers[%d].\n",i));
result = paHostApiInitializers[i]( &hostApis_[hostApisCount_], hostApisCount_ );
- if( result != paNoError )
- goto error;
+ if( result == paNoError ) {
- PA_DEBUG(( "after paHostApiInitializers[%d].\n",i));
+ PA_DEBUG(( "after paHostApiInitializers[%d].\n",i));
- if( hostApis_[hostApisCount_] )
- {
- PaUtilHostApiRepresentation* hostApi = hostApis_[hostApisCount_];
- assert( hostApi->info.defaultInputDevice < hostApi->info.deviceCount );
- assert( hostApi->info.defaultOutputDevice < hostApi->info.deviceCount );
-
- /* the first successfully initialized host API with a default input *or*
- output device is used as the default host API.
- */
- if( (defaultHostApiIndex_ == -1) &&
- ( hostApi->info.defaultInputDevice != paNoDevice
- || hostApi->info.defaultOutputDevice != paNoDevice ) )
+ if( hostApis_[hostApisCount_] )
{
- defaultHostApiIndex_ = hostApisCount_;
- }
+ PaUtilHostApiRepresentation* hostApi = hostApis_[hostApisCount_];
+ assert( hostApi->info.defaultInputDevice < hostApi->info.deviceCount );
+ assert( hostApi->info.defaultOutputDevice < hostApi->info.deviceCount );
+
+ /* the first successfully initialized host API with a default input *or*
+ output device is used as the default host API.
+ */
+ if( (defaultHostApiIndex_ == -1) &&
+ ( hostApi->info.defaultInputDevice != paNoDevice
+ || hostApi->info.defaultOutputDevice != paNoDevice ) )
+ {
+ defaultHostApiIndex_ = hostApisCount_;
+ }
- hostApi->privatePaFrontInfo.baseDeviceIndex = baseDeviceIndex;
+ hostApi->privatePaFrontInfo.baseDeviceIndex = baseDeviceIndex;
- if( hostApi->info.defaultInputDevice != paNoDevice )
- hostApi->info.defaultInputDevice += baseDeviceIndex;
+ if( hostApi->info.defaultInputDevice != paNoDevice )
+ hostApi->info.defaultInputDevice += baseDeviceIndex;
- if( hostApi->info.defaultOutputDevice != paNoDevice )
- hostApi->info.defaultOutputDevice += baseDeviceIndex;
+ if( hostApi->info.defaultOutputDevice != paNoDevice )
+ hostApi->info.defaultOutputDevice += baseDeviceIndex;
- baseDeviceIndex += hostApi->info.deviceCount;
- deviceCount_ += hostApi->info.deviceCount;
+ baseDeviceIndex += hostApi->info.deviceCount;
+ deviceCount_ += hostApi->info.deviceCount;
- ++hostApisCount_;
+ ++hostApisCount_;
+ }
+ }
+ else
+ {
+ PA_DEBUG(( "paHostApiInitializers[%d] failed.\n",i));
}
}
- /* if no host APIs have devices, the default host API is the first initialized host API */
- if( defaultHostApiIndex_ == -1 )
- defaultHostApiIndex_ = 0;
+ if( hostApisCount_ )
+ {
+ /* if no host APIs have devices, the default host API is the first initialized host API */
+ if( defaultHostApiIndex_ == -1 )
+ defaultHostApiIndex_ = 0;
- return result;
+ return paNoError;
+ }
+ else
+ {
+ PA_DEBUG(( "All paHostApiInitializers failed!\n",i));
+ }
error:
TerminateHostApis();
I didn't know that Pulseaudio could be used in Mac OS. Patch is bit polluted as it moves place of sndio also which is not the topic. Why there is NOT APPLE
which makes impossible build it or is this just testing patch?
I didn't know that Pulseaudio could be used in Mac OS.
Judging from the code it also runs on MSWin. I don't know how many projects really need PulseAudio but if enough of them are also ported to the other big 2 platforms it makes sense that PulseAudio runs there too. For me one reasons to run it is that it allows me to play remote audio on my Mac; I would guess that this would work on MSWin too.
And that's also the reason why I filed this ticket: I want to be able to use my regular applications that access CoreAudio devices through PortAudio (QMPlay2, Audacity, ...) regardless of whether I have the PulseAudio daemon running, and without having to activate a different build variant of the PortAudio port (that's a MacPorts feature).
Patch is bit polluted as it moves place of sndio also which is not the topic.
You'd guess that from looking at the patch, but it's just the impression you get because I moved the pulseaudio bit below the platform specific code. And maybe because I changed the logic a bit to do find_package(pulseaudio)
only when the user requests it. That makes interpreting the cmake output more straightforward (no more "whaaat, I didn't ask for PulseAudio support to be built in" ;) )
Why there is
NOT APPLE
which makes impossible build it or is this just testing patch?
Overall this is indeed a patch for my own use that I only attached to show how I obtained PulseAudio support, but I think there's an additional issue here that I didn't want to address properly for now. I may have overlooked something, but there doesn't appear to be a reason to add -DPA_USE_PULSEAUDIO
to the CFLAGS of dependent projects, certainly not to recompile them in entirety (with an invalidated ccache!) if you reinstall portaudio with or without PulseAudio support. If dependents need to be able to check this token (or others) it would be cleaner to put them in a dedicated headerfile which can be included when required. That way only those source files need recompilation.
Libpulse is linked as a private dependency of libportaudio; on Mac ("Darwin"), there is no need to add these libraries to the link command of dependents.
So for me, on my local set-up, the easiest solution for these 2 issues was not to modify the pkgconfig file.
Describe the bug I patched the CMake file and built a Mac portaudio dylib supporting both the native CoreAudio plus PulseAudio, with the idea of being able to use either if and when I have the pulse daemon running (= when needed).
This works, but leaves me with no devices at all as soon as the pulse audio exits.
To Reproduce Steps to reproduce the behavior. Include code if applicable.
Expected behaviour In step 3. I would expect to see the CoreAudio devices
Actual behaviour No devices are shown at all:
Desktop (please complete the following information):
Additional context
At first glance this seems to be cause
InitializeHostApis()
bails when it encounters an error on one of the supported APIs, rather than just skipping it. I'm going to explore that idea.