Issue loading shared object functions with dlsym in C++

405 views Asked by At

I'm trying to load two functions from a shared object (.so) library i had installed, but when i compile my code i get a couple of errors when i try to use those functions, here's my code:

void G729ToPcmFilter::AudioChunkIn(AudioChunkRef& inputAudioChunk){

    std::string s = std::to_string(inputAudioChunk->GetNumBytes());
    LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Size => " + s));

    typedef struct bcg729DecoderChannelContextStruct_struct bcg729DecoderChannelContextStruct;
    void *handle;
    bcg729DecoderChannelContextStruct (*initer)(bcg729DecoderChannelContextStruct);
    void (*decoder)(void);
    char* error;
    handle = dlopen ("libbcg729.so", RTLD_LAZY);
    if (!handle) {
            LOG4CXX_ERROR(LOG.rootLog, CStdString("Couldn't load Bcg729 plugin"));
        }
    else {
            *(void **) (&initer) = dlsym(handle,"initBcg729DecoderChannel");
            *(void **) (&decoder) = dlsym(handle,"bcg729Decoder");
            if ((error = dlerror()) != NULL)  {
                std::string str(error);
                LOG4CXX_ERROR(LOG.rootLog, CStdString("Couldn't load Bcg729 plugin's functions => "+str));
            } else {
                const char* inputBuffer = reinterpret_cast<const char*>((unsigned char*)inputAudioChunk->m_pBuffer);
                char *ret = (char*)malloc(10);
                memcpy(ret, inputBuffer, 10);
                char* firstFragment = ret;
                ret = (char*)malloc(10);
                memcpy(ret, inputBuffer+10, 10);
                char* secondFragment = ret;

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn buffer separated into two 10 bit buffers"));

                int16_t outputBuffer[80]; /* output buffer: the reconstructed signal */ 
                int16_t outputBuffer1[80]; /* output buffer: the reconstructed signal */ 
                uint8_t bitStream[10]; /* binary input for the decoder */

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Buffers Created"));

                bcg729DecoderChannelContextStruct *decoderChannelContext = (*initer)();

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Decoder Initialized"));

                for(int i=0; i < 10 ; i++){
                    bitStream[i] = (uint8_t)atoi(&firstFragment[i]);
                }

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn First BitStream Created"));

                (*decoder)(decoderChannelContext, bitStream, 1, outputBuffer);

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn First Stream Decoding DONE"));

                for(int i=0; i < 10 ; i++){
                    bitStream[i] = (uint8_t)atoi(&secondFragment[i]);
                }

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Second BitStream Created"));

                (*decoder)(decoderChannelContext, bitStream, 0, outputBuffer1);

                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Second Stream Decoding DONE"));

                m_outputAudioChunk.reset(new AudioChunk());
                AudioChunkDetails chunkDetails = *inputAudioChunk->GetDetails();
                chunkDetails.m_rtpPayloadType = -1;
                chunkDetails.m_encoding = PcmAudio;
                chunkDetails.m_numBytes = 160;
                short* outputBufferFinal = (short*)m_outputAudioChunk->CreateBuffer(chunkDetails);
                for(int i = 0; i < 80; i++){
                    outputBufferFinal[i] = outputBuffer[i];
                    outputBufferFinal[i+80] = outputBuffer1[1];
                }

            }
    }
}

I get, the following errors at compile time, I'm sure 'm passing the paramethers as declared on the .so library I'm using:

G729Codec.cpp: In member function ‘virtual void G729ToPcmFilter::AudioChunkIn(AudioChunkRef&)’:
G729Codec.cpp:173:74: error: too few arguments to function
     bcg729DecoderChannelContextStruct *decoderChannelContext = (*initer)();
                                                                          ^
G729Codec.cpp:183:65: error: too many arguments to function
     (*decoder)(decoderChannelContext, bitStream, 1, outputBuffer);
                                                                 ^
G729Codec.cpp:193:66: error: too many arguments to function
     (*decoder)(decoderChannelContext, bitStream, 0, outputBuffer1);

On the header of the libbcg729.so library, the functions are declared as follows:

BCG729_VISIBILITY bcg729DecoderChannelContextStruct *initBcg729DecoderChannel();
BCG729_VISIBILITY void bcg729Decoder(bcg729DecoderChannelContextStruct *decoderChannelContext, uint8_t bitStream[], uint8_t frameErasureFlag, int16_t signal[]);

I can't figure out what I'm doing wrong, can anyone help me?

1

There are 1 answers

8
roalz On

It looks like your declarations of the functions pointers:

bcg729DecoderChannelContextStruct (*initer)(bcg729DecoderChannelContextStruct);
void (*decoder)(void);

are wrong, I suppose they should be something like:

bcg729DecoderChannelContextStruct* (*initer)();
void bcg729Decoder( ... see the bcg729 library header file for the bcg729Decoder() function signature )

You may also want to check if you need to specify those declarations as extern "C", as the bcg729 library is written in C, not C++.