Discussion:
ALSA calls don't work from a shared library
Olivier Guillion - Myriad
2011-08-24 11:36:08 UTC
Permalink
Hi,

I opened a thread yesterday called "ALSA calls don't work within Mozilla plug-
ins", but after more testing, I realize that it is a more global problem, so I
submit another request for help with a proper title.

It seems that several ALSA functions either crash or return unpredictable
values when called from a dynamic library (.so).

I created a very short program that opens a pcm device, sets the sample rate to


44100 and reads the sample rate back to ensure it's correct :
---------------
#include "alsa/asoundlib.h"
static char displayString[1024];

char * ALSATest(void)
{
int err;
snd_pcm_t * handle;
snd_pcm_hw_params_t *params;
unsigned int val,val2;
char status[1024];

err = snd_pcm_open(&handle, "plughw:0,0",SND_PCM_STREAM_PLAYBACK, 0);
if(err>=0)
{
snd_pcm_hw_params_alloca(&params);
snd_pcm_hw_params_any(handle, params);
val=44100;
err=snd_pcm_hw_params_set_rate_near(handle,params, &val, NULL);
val2=0;
err|=snd_pcm_hw_params_get_rate(params, &val2, NULL);
if(val!=44100 || val2!=44100 || err!=0)
sprintf(status,"ALSA is not working (val2=%d err=%d)",val2,err);
else
strcpy(status,"ALSA is OK");
snd_pcm_close(handle);
}
else
strcpy(status,"device cannot be opened");
sprintf(displayString,"ALSA %s\nStatus:%s",snd_asoundlib_version(),status);
return(displayString);
}
-------------

If I call this function directly from the main program, it works well (I get
"ALSA is OK").
Then if I compile it as a shared library (mysharedlib.so), and in another
project I write:

---------------
#include <dlfcn.h>
#include "alsa/asoundlib.h"

typedef char * (*ALSAProc)(void);

int main()
{
void *lib_handle;
void *fn;
char * str;

lib_handle=dlopen("mysharedlib.so",RTLD_NOW);
if (lib_handle)
{
fn=dlsym(lib_handle,"ALSATest");

if(fn) str=((ALSAProc)fn)();
else str="function not found";
}
else str="lib not found";

fprintf(stderr,"%s\n",str);

if(lib_handle) dlclose(lib_handle);
return 0;
}
-------------

I get unpredictable results.
Either;
- it crashes in snd_pcm_hw_params_any (in the subroutine
snd_pcm_hw_params_refine)
or
- it outputs "ALSA is not working (val2=0 err=2147483647)"
or
- it outputs ALSA is not working (val2=2147483647 err=2147483647)"

Does anyone have an idea of what could cause this behaviour?

Olivier
Jaroslav Kysela
2011-08-24 11:42:25 UTC
Permalink
Date 24.8.2011 13:36, Olivier Guillion - Myriad wrote:
>
> Hi,
>
> I opened a thread yesterday called "ALSA calls don't work within Mozilla plug-
> ins", but after more testing, I realize that it is a more global problem, so I
> submit another request for help with a proper title.

It looks like a linking (linker) issue. Are you sure that your dynamic
module is linked with libasound (-lasound option for gcc)?

The command "ldd <your_so_file>" should print libasound.so in list.

Jaroslav

--
Jaroslav Kysela <***@perex.cz>
Linux Kernel Sound Maintainer
ALSA Project; Red Hat, Inc.
Olivier Guillion - Myriad
2011-08-24 14:55:23 UTC
Permalink
Hi,

Jaroslav Kysela wrote :
> It looks like a linking (linker) issue. Are you sure that your dynamic
> module is linked with libasound (-lasound option for gcc)?
>
> The command "ldd <your_so_file>" should print libasound.so in list.

Thank you very much for your answer!
I agree, it's definitely a link problem.

If my main module sends the addresses of the ALSA functions to the shared
library, the test works.
It then means that the shared library by itself doesn't know the right location
of the functions, and that there is a problem during the run-time link.

It has then nothing to do with ALSA, but in my way of managing libraries. :)
I have 15 years of experience in C, but only a couple of months on Linux, and
sometimes I'm a bit lost.

To make ALSA work in my projects (I'm using Code:Blocks), I added to the link
section all the .so libraries I found in /usr/lib/alsa-lib. Apparently it
worked for stand-alone programs, but not for shared libraries.

Could you please tell me what I should add to my project (I'm using
Code:Blocks) to make my programs use the current ALSA shared library in the
system? I tried to build the static lib (./configure --enable-makeshared=no --
enable-static=yes) then the shared lib (./configure --enable-makeshared=no --
enable-static=yes) but it seems to have corrupted my system. My already
compiled old programs don't work anymore, saying that
"snd_async_add_pcm_handler" is not implemented.

Sorry if my request is out of the topic of this mailing-list. Please do not
hesitate to tell me if so.

Olivier
Olivier Guillion - Myriad
2011-08-25 15:31:42 UTC
Permalink
Hi,

I could finally make it work:
- The library corruption problem was due to a faulty symbolic link to an
outdated library.
- I could fix the linker problem by including "/usr/lib/libasound.so.2" in my
link options
- I still got an error sometimes on "snd_async_add_pcm_handler" but I replaced
it by a second thread that fills the pcm buffer when needed.
- The only remaining problem is "snd_device_name_hints" that crashes
inexplicably one out of 5, but I'm working on it.

Thanks for your help!

Olivier


I wrote:
>
> Thank you very much for your answer!
> I agree, it's definitely a link problem.
>
> If my main module sends the addresses of the ALSA functions to the shared
> library, the test works.
> It then means that the shared library by itself doesn't know the right location
> of the functions, and that there is a problem during the run-time link.
>
> It has then nothing to do with ALSA, but in my way of managing libraries. :) I
> have 15 years of experience in C, but only a couple of months on Linux, and
> sometimes I'm a bit lost.
>
> To make ALSA work in my projects (I'm using Code:Blocks), I added to the link
> section all the .so libraries I found in /usr/lib/alsa-lib. Apparently it worked
> for stand-alone programs, but not for shared libraries.
>
> Could you please tell me what I should add to my project (I'm using
> Code:Blocks) to make my programs use the current ALSA shared library in the
> system? I tried to build the static lib (./configure --enable-makeshared=no --
> enable-static=yes) then the shared lib (./configure --enable-makeshared=no --
> enable-static=yes) but it seems to have corrupted my system. My already compiled
> old programs don't work anymore, saying that "snd_async_add_pcm_handler" is not
> implemented.
>
> Sorry if my request is out of the topic of this mailing-list. Please do not
> hesitate to tell me if so.
Continue reading on narkive:
Search results for 'ALSA calls don't work from a shared library' (Questions and Answers)
3
replies
What is Linux?
started 2007-06-21 07:43:04 UTC
software
Loading...