Skip to content

Commit

Permalink
Dynamically load libpulse.so.0 and libasound.so.1 on Linux
Browse files Browse the repository at this point in the history
By generating stubs using https://github.com/hpvb/dynload-wrapper we
can dynamically load libpulse and libasound on systems where it is available.
Both are still a build-time requirement but no longer a run-time dependency.

For maintenance purposes the wrappers should not need to be re-generated
unless we want to bump pulse or asound to an incompatible version. It is
unlikely we will want to do this any time soon.

cherry-pick from 09f82fa
  • Loading branch information
hpvb committed Feb 17, 2021
1 parent 30c69c2 commit 228803e
Show file tree
Hide file tree
Showing 12 changed files with 19,776 additions and 5 deletions.
3 changes: 3 additions & 0 deletions drivers/alsa/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

Import("env")

if "alsa" in env and env["alsa"]:
env.add_source_files(env.drivers_sources, "asound-so_wrap.c")

env.add_source_files(env.drivers_sources, "*.cpp")
11,565 changes: 11,565 additions & 0 deletions drivers/alsa/asound-so_wrap.c

Large diffs are not rendered by default.

3,864 changes: 3,864 additions & 0 deletions drivers/alsa/asound-so_wrap.h

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions drivers/alsa/audio_driver_alsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@

#include <errno.h>

#ifdef PULSEAUDIO_ENABLED
extern "C" {
extern int initialize_pulse();
}
#endif

Error AudioDriverALSA::init_device() {
mix_rate = GLOBAL_GET("audio/mix_rate");
speaker_mode = SPEAKER_MODE_STEREO;
Expand Down Expand Up @@ -147,6 +153,15 @@ Error AudioDriverALSA::init_device() {
}

Error AudioDriverALSA::init() {
#ifdef PULSEAUDIO_ENABLED
// On pulse enabled systems Alsa will silently use pulse.
// It doesn't matter if this fails as that likely means there is no pulse
initialize_pulse();
#endif

if (initialize_asound()) {
return ERR_CANT_OPEN;
}

active = false;
thread_exited = false;
Expand Down
2 changes: 1 addition & 1 deletion drivers/alsa/audio_driver_alsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "core/os/thread.h"
#include "servers/audio_server.h"

#include <alsa/asoundlib.h>
#include "asound-so_wrap.h"

class AudioDriverALSA : public AudioDriver {

Expand Down
3 changes: 3 additions & 0 deletions drivers/pulseaudio/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

Import("env")

if "pulseaudio" in env and env["pulseaudio"]:
env.add_source_files(env.drivers_sources, "pulse-so_wrap.c")

env.add_source_files(env.drivers_sources, "*.cpp")
3 changes: 3 additions & 0 deletions drivers/pulseaudio/audio_driver_pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ Error AudioDriverPulseAudio::init_device() {
}

Error AudioDriverPulseAudio::init() {
if (initialize_pulse()) {
return ERR_CANT_OPEN;
}

active = false;
thread_exited = false;
Expand Down
2 changes: 1 addition & 1 deletion drivers/pulseaudio/audio_driver_pulseaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "core/os/thread.h"
#include "servers/audio_server.h"

#include <pulse/pulseaudio.h>
#include "pulse-so_wrap.h"

class AudioDriverPulseAudio : public AudioDriver {

Expand Down
3,231 changes: 3,231 additions & 0 deletions drivers/pulseaudio/pulse-so_wrap.c

Large diffs are not rendered by default.

1,086 changes: 1,086 additions & 0 deletions drivers/pulseaudio/pulse-so_wrap.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions misc/scripts/clang_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ while IFS= read -rd '' f; do
continue
elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
continue
elif [[ "$f" == *"-so_wrap."* ]]; then
continue
fi

for extension in ${CLANG_FORMAT_FILE_EXTS[@]}; do
Expand Down
5 changes: 2 additions & 3 deletions platform/x11/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,16 @@ def configure(env):

if os.system("pkg-config --exists alsa") == 0: # 0 means found
print("Enabling ALSA")
env["alsa"] = True
env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
# Don't parse --cflags, we don't need to add /usr/include/alsa to include path
env.ParseConfig("pkg-config alsa --libs")
else:
print("ALSA libraries not found, disabling driver")

if env["pulseaudio"]:
if os.system("pkg-config --exists libpulse") == 0: # 0 means found
print("Enabling PulseAudio")
env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
env.ParseConfig("pkg-config --cflags --libs libpulse")
env.ParseConfig("pkg-config --cflags libpulse")
else:
print("PulseAudio development libraries not found, disabling driver")

Expand Down

0 comments on commit 228803e

Please sign in to comment.