From 0ae2f823465ec857b8a8f80af5d28c50aa47d589 Mon Sep 17 00:00:00 2001
From: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
Date: Mon, 4 Feb 2008 16:16:58 -0500
Subject: [PATCH] Bug fix. dlopen uses the installation prefix ( configure ).

The previous commit brought a segmentation fault when launching the client.
When the daemon is lauched separatly, it uses a wrapper script, but it doesn't when the client start the daemon.
SO the LD_LIBRARY_PATH was ignored and the daemon couldn't find the dynamic library for the codecs.
---
 src/Makefile.am         |  5 ++++-
 src/audio/Makefile.am   |  6 +++---
 src/audio/NOTES         | 24 ++++++++++++++++++++++++
 src/audio/audiofile.cpp |  2 +-
 src/audio/audiortp.cpp  |  8 ++++----
 src/iaxvoiplink.cpp     |  8 ++++----
 6 files changed, 40 insertions(+), 13 deletions(-)
 create mode 100644 src/audio/NOTES

diff --git a/src/Makefile.am b/src/Makefile.am
index 45ebfb4529..89b1e5b302 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,7 @@
+sflcodecdir = ($libdir)/sflphone/codecs
 bin_PROGRAMS = sflphoned
 
+
 if USE_ZEROCONF
 ZEROCONFDIR = zeroconf
 ZEROCONFLIB = zeroconf/libzeroconf.la
@@ -39,7 +41,8 @@ sflphoned_CXXFLAGS = -DPREFIX=\"$(prefix)\" -DPROGSHAREDIR=\"${datadir}/sflphone
 
 #sflphoned_LDFLAGS = -static
 sflphoned_LDADD =  ./libsflphone.la $(SFLPHONE_LIBS) $(ZEROCONFLIB) $(LIB_DNSSD) $(IAX_LIBS) $(EXOSIP_LIBS) 
-AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/libs $(libccext2_CFLAGS) $(libccgnu2_CFLAGS) $(IAX_CFLAGS) $(USER_INCLUDES) $(libdbuscpp_CFLAGS)
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/libs $(libccext2_CFLAGS) $(libccgnu2_CFLAGS) $(IAX_CFLAGS) $(USER_INCLUDES) $(libdbuscpp_CFLAGS) \
+	      -DCODECS_DIR=\""$(sflcodecdir)"\" 
 
 
 # libsflphone_la_LDFLAGS= -version-info 0:1:0
diff --git a/src/audio/Makefile.am b/src/audio/Makefile.am
index 53245649f6..47ecdcc556 100644
--- a/src/audio/Makefile.am
+++ b/src/audio/Makefile.am
@@ -1,4 +1,4 @@
-sflcodecdir = $(prefix)/lib/sflphone/codecs/
+sflcodecdir = $(libdir)/sflphone/codecs/
 
 noinst_LTLIBRARIES = libaudio.la
 noinst_PROGRAMS = libcodec_ulaw.so libcodec_alaw.so libcodec_gsm.so
@@ -30,10 +30,10 @@ audiortp.cpp         dtmf.cpp   tone.cpp  audiolayer.cpp audiodevice.cpp  dtmfge
 tonegenerator.cpp   codecDescriptor.cpp \
 audioloop.cpp ringbuffer.cpp $(SPEEX_SOURCES_CPP)
 
-AM_CXXFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/libs $(libccext2_CFLAGS) $(libdbuscpp_CFLAGS) $(libccrtp1_CFLAGS) $(USER_INCLUDES) 
+AM_CXXFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/libs $(libccext2_CFLAGS) $(libdbuscpp_CFLAGS) $(libccrtp1_CFLAGS) $(USER_INCLUDES) \
+ 	      -DCODECS_DIR=\""$(sflcodecdir)"\"	
 libaudio_la_CPPFLAGS = $(SPEEX_FLAG)
 
-
 noinst_HEADERS = audioloop.h       common.h ringbuffer.h           audiofile.h  \
  tonelist.h         audiortp.h audiocodec.h    audiolayer.h audiodevice.h \
  dtmfgenerator.h         tonegenerator.h     \
diff --git a/src/audio/NOTES b/src/audio/NOTES
new file mode 100644
index 0000000000..515353165c
--- /dev/null
+++ b/src/audio/NOTES
@@ -0,0 +1,24 @@
+How to build the codec shared libraries
+---------------------------------------
+
+CODEC_ALAW:
+  gcc -fPIC -g -c -Wall ulaw.cpp         
+-------> gives you the object file ulaw.o. 
+-------> option -g to include debug information
+-------> option -Wall to generate warnings
+-------> option -fPIC to enable position independant code generation
+
+  gcc -shared -o libcodec_ulaw.so ulaw.o -lc
+-------> creates the shared library
+
+CODEC ALAW: 
+  idem
+
+CODEC_GSM:
+  gcc -fPIC -g -c -Wall gsmcodec.cpp
+  gcc -shared -o -libcodec_gsm.so gsmcodec.o -lc -lgsm
+-------> You need the standard library libgsm1 installed (with dev package).
+
+REFERENCES: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
+
+The shared library should be installed in $(libdir)/sflphone/codecs where libdir=$(PREFIX)/lib.
diff --git a/src/audio/audiofile.cpp b/src/audio/audiofile.cpp
index 5e726e8d2f..7e4dca105c 100644
--- a/src/audio/audiofile.cpp
+++ b/src/audio/audiofile.cpp
@@ -35,7 +35,7 @@ AudioFile::AudioFile()
 
    using std::cout;
    using std::cerr;
-   void* codec = dlopen("libcodec_ulaw.so", RTLD_LAZY);
+   void* codec = dlopen( CODECS_DIR "/libcodec_ulaw.so", RTLD_LAZY);
    if(!codec){
         cerr<<"cannot load library: "<< dlerror() <<'\n';
    }
diff --git a/src/audio/audiortp.cpp b/src/audio/audiortp.cpp
index d71c7268a5..13c787961d 100644
--- a/src/audio/audiortp.cpp
+++ b/src/audio/audiortp.cpp
@@ -259,16 +259,16 @@ AudioRtpRTX::loadCodec(int payload)
 
 	switch(payload){
 	  case 0:
-            handle_codec = dlopen("libcodec_ulaw.so", RTLD_LAZY);
+            handle_codec = dlopen( CODECS_DIR "/libcodec_ulaw.so", RTLD_LAZY);
 	    break;
 	  case 3:
-	    handle_codec = dlopen("libcodec_gsm.so", RTLD_LAZY);
+	    handle_codec = dlopen(CODECS_DIR "/libcodec_gsm.so", RTLD_LAZY);
 	    break;
 	  case 8:
-	    handle_codec = dlopen("libcodec_alaw.so", RTLD_LAZY);
+	    handle_codec = dlopen(CODECS_DIR "/libcodec_alaw.so", RTLD_LAZY);
 	    break;
 	  case 97:
-            handle_codec = dlopen("libcodec_ilbc.so", RTLD_LAZY);
+            handle_codec = dlopen(CODECS_DIR "/libcodec_ilbc.so", RTLD_LAZY);
 	    break;
 	}
 
diff --git a/src/iaxvoiplink.cpp b/src/iaxvoiplink.cpp
index 7eb33facdd..4e4738deb1 100644
--- a/src/iaxvoiplink.cpp
+++ b/src/iaxvoiplink.cpp
@@ -236,16 +236,16 @@ IAXVoIPLink::loadCodec(int payload)
    switch(payload)
    {
      case 0:
-       handle_codec = dlopen("libcodec_ulaw.so", RTLD_LAZY);
+       handle_codec = dlopen(CODECS_DIR "/libcodec_ulaw.so", RTLD_LAZY);
        break;
      case 3:
-       handle_codec = dlopen("libcodec_gsm.so", RTLD_LAZY);
+       handle_codec = dlopen(CODECS_DIR "/libcodec_gsm.so", RTLD_LAZY);
        break;
      case 8:
-       handle_codec = dlopen("libcodec_alaw.so", RTLD_LAZY);
+       handle_codec = dlopen(CODECS_DIR "/libcodec_alaw.so", RTLD_LAZY);
        break;
      case 97:
-       handle_codec = dlopen("libcodec_ilbc.so", RTLD_LAZY);
+       handle_codec = dlopen(CODECS_DIR "/libcodec_ilbc.so", RTLD_LAZY);
        break;
    }
    if(!handle_codec){
-- 
GitLab