Changeset 4226


Ignore:
Timestamp:
Aug 10, 2012 2:52:34 AM (12 years ago)
Author:
ming
Message:

Re #1518: Add support for cross-platform OpenSL audio device (thanks to Regis for the code)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/android/pjmedia/src/pjmedia-audiodev/opensl_dev.c

    r4215 r4226  
    3434 
    3535#include <SLES/OpenSLES.h> 
    36 #include <SLES/OpenSLES_Android.h> 
    37 #include <sys/system_properties.h> 
    38 #include <android/api-level.h> 
     36 
     37#ifdef __ANDROID__ 
     38    #include <SLES/OpenSLES_Android.h> 
     39    #include <SLES/OpenSLES_AndroidConfiguration.h> 
     40    #include <sys/system_properties.h> 
     41    #include <android/api-level.h> 
     42 
     43    #define W_SLBufferQueueItf SLAndroidSimpleBufferQueueItf 
     44    #define W_SLBufferQueueState SLAndroidSimpleBufferQueueState 
     45    #define W_SL_IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE 
     46#else 
     47    #define W_SLBufferQueueItf SLBufferQueueItf 
     48    #define W_SLBufferQueueState SLBufferQueueState 
     49    #define W_SL_IID_BUFFERQUEUE SL_IID_BUFFERQUEUE 
     50#endif 
    3951 
    4052#define THIS_FILE       "opensl_dev.c" 
     
    98110    int                 recordBufIdx; 
    99111 
    100     SLAndroidSimpleBufferQueueItf playerBufQ; 
    101     SLAndroidSimpleBufferQueueItf recordBufQ; 
     112    W_SLBufferQueueItf playerBufQ; 
     113    W_SLBufferQueueItf recordBufQ; 
    102114}; 
    103115 
     
    155167 
    156168/* This callback is called every time a buffer finishes playing. */ 
    157 void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) 
     169void bqPlayerCallback(W_SLBufferQueueItf bq, void *context) 
    158170{ 
    159171    struct opensl_aud_stream *stream = (struct opensl_aud_stream*) context; 
     
    201213 
    202214/* This callback handler is called every time a buffer finishes recording */ 
    203 void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context) 
     215void bqRecorderCallback(W_SLBufferQueueItf bq, void *context) 
    204216{ 
    205217    struct opensl_aud_stream *stream = (struct opensl_aud_stream*) context; 
     
    449461{ 
    450462    /* Audio sink for recorder and audio source for player */ 
     463#ifdef __ANDROID__ 
    451464    SLDataLocator_AndroidSimpleBufferQueue loc_bq = 
    452465        { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS }; 
     466#else 
     467    SLDataLocator_BufferQueue loc_bq = 
     468        { SL_DATALOCATOR_BUFFERQUEUE, NUM_BUFFERS }; 
     469#endif 
    453470    struct opensl_aud_factory *pa = (struct opensl_aud_factory*)f; 
    454471    pj_pool_t *pool; 
     
    497514        SLDataSink audioSnk = {&loc_outmix, NULL}; 
    498515        /* Audio interface */ 
     516#ifdef __ANDROID__ 
     517        int numIface = 3; 
    499518        const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, 
    500519                                      SL_IID_VOLUME, 
     
    504523        SLAndroidConfigurationItf playerConfig; 
    505524        SLint32 streamType = SL_ANDROID_STREAM_VOICE; 
     525#else 
     526        int numIface = 2; 
     527        const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, 
     528                                      SL_IID_VOLUME}; 
     529        const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; 
     530#endif 
    506531         
    507532        /* Create audio player */ 
     
    509534                                                        &stream->playerObj, 
    510535                                                        &audioSrc, &audioSnk, 
    511                                                         3, ids, req); 
     536                                                        numIface, ids, req); 
    512537        if (result != SL_RESULT_SUCCESS) { 
    513538            PJ_LOG(3, (THIS_FILE, "Cannot create audio player: %d", result)); 
     
    515540        } 
    516541 
     542#ifdef __ANDROID__ 
    517543        /* Set Android configuration */ 
    518544        result = (*stream->playerObj)->GetInterface(stream->playerObj, 
     
    528554                                  "player configuration")); 
    529555        } 
    530          
     556#endif 
     557 
    531558        /* Realize the player */ 
    532559        result = (*stream->playerObj)->Realize(stream->playerObj, 
     
    587614        SLDataSink audioSnk = {&loc_bq, &format_pcm}; 
    588615        /* Audio interface */ 
    589         const SLInterfaceID ids[2] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 
     616#ifdef __ANDROID__ 
     617        int numIface = 2; 
     618        const SLInterfaceID ids[2] = {W_SL_IID_BUFFERQUEUE, 
    590619                                      SL_IID_ANDROIDCONFIGURATION}; 
    591620        const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_FALSE}; 
    592621        SLAndroidConfigurationItf recorderConfig; 
     622#else 
     623        int numIface = 1; 
     624        const SLInterfaceID ids[1] = {W_SL_IID_BUFFERQUEUE}; 
     625        const SLboolean req[1] = {SL_BOOLEAN_TRUE}; 
     626#endif 
    593627         
    594628        /* Create audio recorder 
     
    598632                                                          &stream->recordObj, 
    599633                                                          &audioSrc, &audioSnk, 
    600                                                           2, ids, req); 
     634                                                          numIface, ids, req); 
    601635        if (result != SL_RESULT_SUCCESS) { 
    602636            PJ_LOG(3, (THIS_FILE, "Cannot create recorder: %d", result)); 
     
    604638        } 
    605639 
     640#ifdef __ANDROID__ 
    606641        /* Set Android configuration */ 
    607642        result = (*stream->recordObj)->GetInterface(stream->recordObj, 
     
    631666                                  "recorder configuration")); 
    632667        } 
     668#endif 
    633669         
    634670        /* Realize the recorder */ 
     
    651687        /* Get the buffer queue interface */ 
    652688        result = (*stream->recordObj)->GetInterface( 
    653                      stream->recordObj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, 
     689                     stream->recordObj, W_SL_IID_BUFFERQUEUE, 
    654690                     &stream->recordBufQ); 
    655691        if (result != SL_RESULT_SUCCESS) { 
     
    861897/*       
    862898        SLresult result; 
    863         SLAndroidSimpleBufferQueueState state; 
     899        W_SLBufferQueueState state; 
    864900 
    865901        result = (*stream->playerBufQ)->GetState(stream->playerBufQ, &state); 
Note: See TracChangeset for help on using the changeset viewer.