Commit 9bdb348a authored by Rafaël Carré's avatar Rafaël Carré
Browse files

AudioCodecFactory : cleanup

parent fac40987
......@@ -37,8 +37,7 @@
#include "audiocodecfactory.h"
#include "fileutils.h"
AudioCodecFactory::AudioCodecFactory() : _CodecsMap(), _defaultCodecOrder(), _Cache(), _nbCodecs(), _CodecInMemory()
AudioCodecFactory::AudioCodecFactory() : _CodecsMap(), _defaultCodecOrder(), _Cache(), _CodecInMemory()
{
}
......@@ -47,44 +46,25 @@ AudioCodecFactory::~AudioCodecFactory()
}
//std::vector<std::string> CodecDescriptor::getAllMimeSubtypes()
//{
// std::vector<std::string> output;
// IdentifierToCodecInstanceMapIterator it;
//
// for (it = _codecsMap.begin(); it != _codecsMap.end(); it++) {
// output.push_back ( ( (*it).second)->getMimeSubtype());
// }
//
// return output;
//}
void
AudioCodecFactory::init()
{
std::vector<sfl::Codec*> CodecDynamicList = scanCodecDirectory();
_nbCodecs = CodecDynamicList.size();
if (_nbCodecs <= 0)
_error ("CodecDescriptor: Error - No codecs available");
if (CodecDynamicList.size() == 0)
_error ("Error - No codecs available");
for (int i = 0 ; i < _nbCodecs ; i++) {
for (size_t i = 0 ; i < CodecDynamicList.size() ; i++) {
_CodecsMap[ (AudioCodecType) CodecDynamicList[i]->getPayloadType() ] = CodecDynamicList[i];
_debug ("CodecDescriptor: %s" , CodecDynamicList[i]->getMimeSubtype().c_str());
_debug ("Loaded codec %s" , CodecDynamicList[i]->getMimeSubtype().c_str());
}
}
void AudioCodecFactory::setDefaultOrder()
{
_defaultCodecOrder.clear();
CodecsMap::iterator iter = _CodecsMap.begin();
while (iter != _CodecsMap.end()) {
CodecsMap::iterator iter;
for (iter = _CodecsMap.begin(); iter != _CodecsMap.end(); ++iter)
_defaultCodecOrder.push_back (iter->first);
iter ++ ;
}
}
std::string
......@@ -93,9 +73,8 @@ AudioCodecFactory::getCodecName (AudioCodecType payload)
std::string resNull = "";
CodecsMap::iterator iter = _CodecsMap.find (payload);
if (iter!=_CodecsMap.end()) {
if (iter!=_CodecsMap.end())
return (iter->second->getMimeSubtype());
}
return resNull;
}
......@@ -108,20 +87,19 @@ AudioCodecFactory::getCodec (AudioCodecType payload)
if (iter != _CodecsMap.end())
return iter->second;
_error ("CodecDescriptor: Error cannont found codec %i in _CodecsMap from codec descriptor", payload);
_error ("CodecDescriptor: cannot find codec %i", payload);
return NULL;
}
double AudioCodecFactory::getBitRate (AudioCodecType payload)
{
CodecsMap::iterator iter = _CodecsMap.find (payload);
if (iter!=_CodecsMap.end())
return (iter->second->getBitRate());
else
return 0.0;
return 0.0;
}
......@@ -132,37 +110,27 @@ int AudioCodecFactory::getSampleRate (AudioCodecType payload)
if (iter!=_CodecsMap.end())
return (iter->second->getClockRate());
else
return 0;
return 0;
}
void AudioCodecFactory::saveActiveCodecs (const std::vector<std::string>& list)
{
_defaultCodecOrder.clear();
// list contains the ordered payload of active codecs picked by the user
// we used the CodecOrder vector to save the order.
int i=0;
int payload;
size_t size = list.size();
while ( (unsigned int) i < size) {
payload = std::atoi (list[i].data());
if (isCodecLoaded (payload)) {
for (size_t i = 0; i < list.size(); i++) {
int payload = std::atoi (list[i].data());
if (isCodecLoaded (payload))
_defaultCodecOrder.push_back ( (AudioCodecType) payload);
}
i++;
}
}
void
AudioCodecFactory::deleteHandlePointer (void)
{
_debug ("CodecDesccriptor: Delete codec handle pointers");
for (int i = 0 ; (unsigned int) i < _CodecInMemory.size() ; i++) {
for (size_t i = 0 ; i < _CodecInMemory.size() ; i++) {
unloadCodec (_CodecInMemory[i]);
}
......@@ -172,37 +140,33 @@ AudioCodecFactory::deleteHandlePointer (void)
std::vector<sfl::Codec*> AudioCodecFactory::scanCodecDirectory (void)
{
std::vector<sfl::Codec*> codecs;
std::vector<std::string> dirToScan;
std::string libDir = std::string (CODECS_DIR).append ("/");
std::string homeDir = std::string (HOMEDIR) + DIR_SEPARATOR_STR + "." + PROGDIR + "/";
const char *progDir = get_program_dir();
// look for a CODECS_PATH environment variable...used in tests
dirToScan.push_back (std::string(HOMEDIR) + DIR_SEPARATOR_STR "." PROGDIR "/");
dirToScan.push_back (CODECS_DIR "/");
const char *envDir = getenv("CODECS_PATH");
std::vector<std::string> dirToScan;
dirToScan.push_back (homeDir);
dirToScan.push_back (libDir);
if (envDir)
dirToScan.push_back(std::string(envDir) + DIR_SEPARATOR_STR);
const char *progDir = get_program_dir();
if (progDir)
dirToScan.push_back(std::string(progDir) + DIR_SEPARATOR_STR + "audio/codecs/");
for (int i = 0 ; (unsigned int) i < dirToScan.size() ; i++) {
for (size_t i = 0 ; i < dirToScan.size() ; i++) {
std::string dirStr = dirToScan[i];
_debug ("CodecDescriptor: Scanning %s to find audio codecs....", dirStr.c_str());
DIR *dir = opendir (dirStr.c_str());
sfl::Codec* audioCodec;
DIR *dir = opendir (dirStr.c_str());
if (!dir)
continue;
dirent *dirStruct;
while ( (dirStruct = readdir (dir))) {
std::string file = dirStruct -> d_name ;
std::string file = dirStruct->d_name ;
if (file == CURRENT_DIR or file == PARENT_DIR)
continue;
if (seemsValid (file) && !alreadyInCache (file)) {
audioCodec = loadCodec (dirStr+file);
sfl::Codec* audioCodec = loadCodec (dirStr+file);
if (audioCodec) {
codecs.push_back (audioCodec);
_Cache.push_back (file);
......@@ -218,10 +182,7 @@ std::vector<sfl::Codec*> AudioCodecFactory::scanCodecDirectory (void)
sfl::Codec* AudioCodecFactory::loadCodec (std::string path)
{
CodecHandlePointer p;
void * codecHandle = dlopen (path.c_str() , RTLD_LAZY);
if (!codecHandle) {
_error("%s\n", dlerror());
return NULL;
......@@ -231,14 +192,14 @@ sfl::Codec* AudioCodecFactory::loadCodec (std::string path)
create_t* createCodec = (create_t*) dlsym (codecHandle , "create");
char *error = dlerror();
if (error)
if (error) {
_error("%s\n", error);
return NULL;
}
sfl::Codec* a = createCodec();
p = CodecHandlePointer (a, codecHandle);
_CodecInMemory.push_back (p);
_CodecInMemory.push_back (CodecHandlePointer (a, codecHandle));
return a;
}
......@@ -246,12 +207,13 @@ sfl::Codec* AudioCodecFactory::loadCodec (std::string path)
void AudioCodecFactory::unloadCodec (CodecHandlePointer p)
{
destroy_t* destroyCodec = (destroy_t*) dlsym (p.second , "destroy");
char *error = dlerror();
if (error)
if (error) {
_error("%s\n", error);
return;
}
destroyCodec (p.first);
......@@ -281,7 +243,6 @@ sfl::Codec* AudioCodecFactory::instantiateCodec (AudioCodecType payload)
sfl::Codec* AudioCodecFactory::getFirstCodecAvailable (void)
{
CodecsMap::iterator iter = _CodecsMap.begin();
if (iter != _CodecsMap.end())
......@@ -292,93 +253,65 @@ sfl::Codec* AudioCodecFactory::getFirstCodecAvailable (void)
bool AudioCodecFactory::seemsValid (std::string lib)
{
// The name of the shared library seems valid <==> it looks like libcodec_xxx.so
// We check this
std::string begin = SFL_CODEC_VALID_PREFIX;
std::string end = SFL_CODEC_VALID_EXTEN;
// First : check the length of the file name.
// If it is shorter than begin.length() + end.length() , not a SFL shared library
std::string prefix = SFL_CODEC_VALID_PREFIX;
std::string suffix = SFL_CODEC_VALID_EXTEN;
if (lib.length() <= begin.length() + end.length())
ssize_t len = lib.length() - prefix.length() - suffix.length();
if (len < 0)
return false;
// Second: check the extension of the file name.
// If it is different than SFL_CODEC_VALID_EXTEN , not a SFL shared library
if (lib.substr (lib.length() - end.length() , lib.length()) != end)
if (lib.substr (lib.length() - suffix.length() , lib.length()) != suffix)
return false;
#ifdef HAVE_SPEEX_CODEC
// Nothing special
#else
if (lib.substr (begin.length() , lib.length() - begin.length() - end.length()) == SPEEX_STRING_DESCRIPTION)
#ifndef HAVE_SPEEX_CODEC
if (lib.substr (prefix.length() , len) == SPEEX_STRING_DESCRIPTION)
return false;
#endif
#ifdef HAVE_GSM_CODEC
// Nothing special
#else
if (lib.substr (begin.length() , lib.length() - begin.length() - end.length()) == GSM_STRING_DESCRIPTION)
#ifndef HAVE_GSM_CODEC
if (lib.substr (prefix.length() , len) == GSM_STRING_DESCRIPTION)
return false;
#endif
#ifdef BUILD_ILBC
// Nothing special
#else
if (lib.substr (begin.length() , lib.length() - begin.length() - end.length()) == ILBC_STRING_DESCRIPTION)
#ifndef BUILD_ILBC
if (lib.substr (prefix.length() , len) == ILBC_STRING_DESCRIPTION)
return false;
#endif
if (lib.substr (0, begin.length()) == begin)
if (lib.substr (lib.length() - end.length() , end.length()) == end)
if (lib.substr (0, prefix.length()) == prefix)
if (lib.substr (lib.length() - suffix.length() , suffix.length()) == suffix)
return true;
else
return false;
else
return false;
return false;
}
bool
AudioCodecFactory::alreadyInCache (std::string lib)
{
int i;
for (i = 0 ; (unsigned int) i < _Cache.size() ; i++) {
if (_Cache[i] == lib) {
for (size_t i = 0 ; i < _Cache.size() ; i++)
if (_Cache[i] == lib)
return true;
}
}
return false;
}
bool AudioCodecFactory::isCodecLoaded (int payload)
{
CodecsMap::iterator iter = _CodecsMap.begin();
while (iter != _CodecsMap.end()) {
CodecsMap::iterator iter;
for (iter = _CodecsMap.begin(); iter != _CodecsMap.end(); ++iter)
if (iter -> first == payload)
return true;
iter++;
}
return false;
}
std::vector <std::string> AudioCodecFactory::getCodecSpecifications (const int32_t& payload)
{
_debug ("CodecDescriptor: Gathering codec specifications for payload %i", payload);
std::vector<std::string> v;
std::stringstream ss;
......
......@@ -69,7 +69,7 @@ class AudioCodecFactory
* Accessor to data structures
* @return CodecsMap& The available codec
*/
CodecsMap& getCodecsMap() {
const CodecsMap& getCodecsMap() const {
return _CodecsMap;
}
......@@ -119,14 +119,6 @@ class AudioCodecFactory
*/
void saveActiveCodecs (const std::vector<std::string>& list);
/**
* Get the number of codecs loaded in dynamic memory
* @return int The number
*/
int getCodecsNumber (void) {
return _nbCodecs;
}
/**
* Unreferences the codecs loaded in memory
*/
......@@ -215,11 +207,6 @@ class AudioCodecFactory
*/
std::vector<std::string> _Cache;
/**
* Number of codecs loaded
*/
int _nbCodecs;
/**
* Vector containing pairs
* Pair between pointer on function handle and pointer on audiocodec object
......
......@@ -159,8 +159,8 @@ typedef enum {
/** The struct to reflect the order the user wants to use the codecs */
typedef std::vector<AudioCodecType> CodecOrder;
const char * const IP2IP_PROFILE = "IP2IP";
const char * const DIR_SEPARATOR_STR = "/"; // Directory separator char
const char DIR_SEPARATOR_CH = '/'; /** Directory separator string */
#define IP2IP_PROFILE "IP2IP"
#define DIR_SEPARATOR_STR "/" // Directory separator char
#define DIR_SEPARATOR_CH = '/' /** Directory separator string */
#endif // __GLOBAL_H__
......@@ -97,7 +97,7 @@ void ManagerImpl::init ()
selectAudioDriver();
// Initialize the list of supported audio codecs
initAudioCodec();
_audioCodecFactory.init();
audioLayerMutexLock();
......@@ -2067,7 +2067,7 @@ void ManagerImpl::ringtone (const std::string& accountID)
}
std::string ringchoice = account->getRingtonePath();
if (ringchoice.find (DIR_SEPARATOR_CH) == std::string::npos) {
if (ringchoice.find (DIR_SEPARATOR_STR) == std::string::npos) {
// check inside global share directory
ringchoice = std::string (PROGSHAREDIR) + DIR_SEPARATOR_STR
+ RINGDIR + DIR_SEPARATOR_STR + ringchoice;
......@@ -2259,19 +2259,6 @@ void ManagerImpl::initConfigFile (std::string alternate)
}
}
/**
* Initialization: Main Thread
*/
void ManagerImpl::initAudioCodec (void)
{
_info ("Manager: Init audio codecs");
/* Init list of all supported codecs by the application.
* This is a global list. Every account will inherit it.
*/
_audioCodecFactory.init();
}
std::vector<std::string> ManagerImpl::unserialize (std::string s)
{
std::vector<std::string> list;
......
......@@ -1041,11 +1041,6 @@ class ManagerImpl
*/
std::string getConfigFile (void) const;
/*
* Initialize audiocodec with config setting
*/
void initAudioCodec (void);
/*
* Initialize zeroconf module and scanning
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment