google / oboe

Oboe is a C++ library that makes it easy to build high-performance audio apps on Android.
Apache License 2.0
3.71k stars 569 forks source link

Static linking with Oboe 1.9.0 causes OpenSL to crash #2102

Closed dk-3cx closed 1 day ago

dk-3cx commented 1 month ago

Oboe version: 1.9.0 Doesn't reproduce in 1.8.1

The recent change in src/opensles/EngineOpenSLES.cpp included into Oboe 1.9.0 has introduced stubs for OpenSL ES global constants inside the Oboe library and initializes them all with nullptr:

// Satisfy extern in OpenSLES.h
// These are required because of b/337360630, which was causing
// Oboe to have link failures if libOpenSLES.so was not available.
SL_API const SLInterfaceID SL_IID_ENGINE = nullptr;
SL_API const SLInterfaceID SL_IID_ANDROIDSIMPLEBUFFERQUEUE = nullptr;
SL_API const SLInterfaceID SL_IID_ANDROIDCONFIGURATION = nullptr;
SL_API const SLInterfaceID SL_IID_RECORD = nullptr;
SL_API const SLInterfaceID SL_IID_BUFFERQUEUE = nullptr;
SL_API const SLInterfaceID SL_IID_VOLUME = nullptr;
SL_API const SLInterfaceID SL_IID_PLAY = nullptr;

We develop an Android app (in Kotlin) with a native library for it that uses both Oboe and OpenSL. OpenSL is used NOT as a backend for Oboe, but independently from Oboe. Oboe is statically linked with our native lib, while libOpenSLES.so is used as a shared library. This leads to crashes due to null pointer exceptions, because the stubs in Oboe hide the original constants from libOpenSLES.so.

Could you please fix it in some way, e.g. by introducing some #ifdef with a macro that can be passed to cmake when building Oboe? So that we can choose during the compilation if we need these stubs or not.

philburk commented 1 month ago

Hmm. We did not anticipate that. I try to avoid conditional compilation tricks. I think it might work if I change SL_IID_ENGINE to OBOE_SL_IID_ENGINE, etcetera.

@dk-3cx - Do you think that would be enough to avoid the naming collisions?

dk-3cx commented 1 month ago

Our code works fine if I remove all the mentioned constants. Renaming them seems to have the same effect. But is such a solution acceptable from your side? I guess you named them this way for a reason, no? (to satisfy some extern declarations)

buliaoyin commented 3 weeks ago

We encountered the same issue. The definitions in src/opensles/EngineOpenSLES.cpp caused errors in other code that directly uses OpenSL ES. For now, we are also temporarily removing this section to bypass the problem.

philburk commented 3 weeks ago

I guess you named them this way for a reason, no? (to satisfy some extern declarations)

Right. So renaming them would fix your problem but unfix the original problem when libOpenSLes.so is missing.

I coulld add a #ifndef around SL_IID_ENGINE and the other stubs as you suggested. I wish there was some way to use link time information to make compile time decisions. But it sems like the order is wrong.

We will look for some alternative but we will probably go with the #ifndef. We want to move carefully to avoid further breakage.