diff --git a/src/audio/audiortp.cpp b/src/audio/audiortp.cpp index e4b16cbdfa3069548da249b315dfb3841b575ad5..ba6ffb880e51a16183a77d0f2c55023c9c31c5f6 100644 --- a/src/audio/audiortp.cpp +++ b/src/audio/audiortp.cpp @@ -258,45 +258,49 @@ AudioRtpRTX::initAudioRtpSession (void) AudioCodec* AudioRtpRTX::loadCodec(int payload) { - using std::cout; using std::cerr; - void* codec; switch(payload){ case 0: - codec = dlopen("codec_ulaw.so", RTLD_LAZY); + handle_codec = dlopen("codec_ulaw.so", RTLD_LAZY); break; case 3: - codec = dlopen("codec_gsm.so", RTLD_LAZY); + handle_codec = dlopen("codec_gsm.so", RTLD_LAZY); break; case 8: - codec = dlopen("codec_alaw.so", RTLD_LAZY); + handle_codec = dlopen("codec_alaw.so", RTLD_LAZY); break; } - if(!codec){ + if(!handle_codec){ cerr<<"cannot load library: "<< dlerror() <<'\n'; } dlerror(); - create_t* create_codec = (create_t*)dlsym(codec, "create"); + create_t* create_codec = (create_t*)dlsym(handle_codec, "create"); const char* dlsym_error = dlerror(); if(dlsym_error){ cerr << "Cannot load symbol create: " << dlsym_error << '\n'; } - destroy_t* destroy_codec = (destroy_t*) dlsym(codec, "destroy"); - dlsym_error = dlerror(); - if(dlsym_error){ - cerr << "Cannot load symbol destroy" << dlsym_error << '\n'; - } return create_codec(); +} +void +AudioRtpRTX::unloadCodec(AudioCodec* audiocodec) +{ + using std::cerr; + destroy_t* destroy_codec = (destroy_t*)dlsym(handle_codec, "destroy"); + const char* dlsym_error = dlerror(); + if(dlsym_error){ + cerr << "Cannot load symbol destroy" << dlsym_error << '\n'; + } + destroy_codec(audiocodec); + dlclose(handle_codec); } void AudioRtpRTX::sendSessionFromMic(int timestamp) { - AudioCodec* audiocodec = loadCodec(_ca->getAudioCodec()); // STEP: @@ -358,10 +362,7 @@ try { _debugException("! ARTP: sending failed"); throw; } - - -//destroy_codec(audiocodec); -//dlclose(codec); + unloadCodec(audiocodec); } @@ -369,6 +370,9 @@ try { void AudioRtpRTX::receiveSessionForSpkr (int& countTime) { + +AudioCodec* audiocodec; + if (_ca == 0) { return; } try { AudioLayer* audiolayer = Manager::instance().getAudioDriver(); @@ -391,33 +395,8 @@ try { unsigned char* data = (unsigned char*)adu->getData(); // data in char unsigned int size = adu->getSize(); // size in char -/*using std::cout; -using std::cerr; -void* codec = dlopen("codec_alaw.so", RTLD_LAZY); -if(!codec){ - cerr<<"cannot load library: "<< dlerror() <<'\n'; -} - -//reset errors -dlerror(); - -//load the symbols -create_t* create_codec = (create_t*)dlsym(codec, "create"); -const char* dlsym_error = dlerror(); -if(dlsym_error){ - cerr << "Cannot load symbol create: " << dlsym_error << '\n'; -} -destroy_t* destroy_codec = (destroy_t*) dlsym(codec, "destroy"); -dlsym_error = dlerror(); -if(dlsym_error){ - cerr << "Cannot load symbol destroy" << dlsym_error << '\n'; -} - -AudioCodec* audiocodec = create_codec(); -*/ -AudioCodec* audiocodec = loadCodec(payload); + audiocodec = loadCodec(payload); // Decode data with relevant codec - //AudioCodec* audiocodec = _ca->getCodecMap().getCodec((CodecType)payload); _codecSampleRate = audiocodec->getClockRate(); int max = (int)(_codecSampleRate * _layerFrameSize); @@ -467,14 +446,12 @@ AudioCodec* audiocodec = loadCodec(payload); } delete adu; adu = NULL; -//destroy_codec(audiocodec); -//dlclose(codec); } catch(...) { _debugException("! ARTP: receiving failed"); throw; } - + unloadCodec(audiocodec); } diff --git a/src/audio/audiortp.h b/src/audio/audiortp.h index 09dda4c2054d14260c6aeee8cd28b6d19fff5780..c37174826e3e6e4eee7339ba0bdb2e754d7e95e0 100644 --- a/src/audio/audiortp.h +++ b/src/audio/audiortp.h @@ -120,8 +120,21 @@ class AudioRtpRTX : public ost::Thread, public ost::TimerPort { */ int downSampleData(int, int); - + /** Pointer on function to handle codecs **/ + void* handle_codec; + + /** + * Load dynamically a codec (.so library) + * @param payload The payload of the codec you want to load + * @return AudioCodec* A pointer on a audio codec object + */ AudioCodec* loadCodec(int payload); + + /** + * Destroy and close dynamically a codec (.so library) + * @param audiocodec The audio codec you want to unload + */ + void unloadCodec(AudioCodec* audiocodec); }; /////////////////////////////////////////////////////////////////////////////// diff --git a/src/iaxvoiplink.cpp b/src/iaxvoiplink.cpp index de824d06d9d2b816397dd3dde05d82ff3ac67db3..90054839a693d07afb1d87f5c738e3a82ac0c53f 100644 --- a/src/iaxvoiplink.cpp +++ b/src/iaxvoiplink.cpp @@ -231,9 +231,7 @@ IAXVoIPLink::getEvent() AudioCodec* IAXVoIPLink::loadCodec(int payload) { - using std::cout; using std::cerr; - void* handle_codec; switch(payload) { @@ -259,12 +257,21 @@ IAXVoIPLink::loadCodec(int payload) if(dlsym_error){ cerr << "Cannot load symbol create: " << dlsym_error << '\n'; } + return create_codec(); +} + +void +IAXVoIPLink::unloadCodec(AudioCodec* audiocodec) +{ + using std::cerr; + destroy_t* destroy_codec = (destroy_t*) dlsym(handle_codec, "destroy"); - dlsym_error = dlerror(); + const char* dlsym_error = dlerror(); if(dlsym_error){ cerr << "Cannot load symbol destroy" << dlsym_error << '\n'; } - return create_codec(); + destroy_codec(audiocodec); + dlclose(handle_codec); } void @@ -404,9 +411,7 @@ IAXVoIPLink::sendAudioFromMic(void) } _mutexIAX.leaveMutex(); } - - //destroy_codec(audiocodec); - //dlclose(codec); + unloadCodec(audiocodec); } @@ -867,9 +872,7 @@ AudioCodec* audiocodec; } else { _debug("IAX: incoming audio, but no sound card open"); } -//destroy_codec(audiocodec); -//dlclose(codec); - + unloadCodec(audiocodec); } diff --git a/src/iaxvoiplink.h b/src/iaxvoiplink.h index 02e910c3428a19325ba5aa571ae065873a942973..c0fe8c30a83224db7ca791d4534838c849323a3c 100644 --- a/src/iaxvoiplink.h +++ b/src/iaxvoiplink.h @@ -158,6 +158,15 @@ private: */ AudioCodec* loadCodec(int payload); + /** + * Destroy and close the pointer on the codec + * @param audiocodec the codec you want to unload + */ + void unloadCodec(AudioCodec* audiocodec); + + /** pointer on function **/ + void* handle_codec; + /** Threading object */ EventThread* _evThread; diff --git a/src/sipcall.cpp b/src/sipcall.cpp index 0b01f94626136aabac2c14300d1d7f46a48efa48..b81b0773b9675c5444da132c3df0ac4e32f28b6d 100644 --- a/src/sipcall.cpp +++ b/src/sipcall.cpp @@ -599,7 +599,6 @@ SIPCall::setAudioCodecFromSDP(sdp_media_t* remote_med, int tid) int payload = atoi (tmp); _debug(" Payload: %d\n", payload); setAudioCodec((CodecType)payload); // codec builder for the mic - _debug("SetAUDIOcodec!!\n"); } if (getAudioCodec() == (CodecType) -1) { _debug("SIPCall Failure: Unable to set codec\n");