Ignore:
Timestamp:
Mar 11, 2011 6:57:24 AM (11 years ago)
Author:
ming
Message:

Fixed #1204: Support for refreshing audio device list.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia-audiodev/alsa_dev.c

    r3079 r3438  
    5858static pj_status_t alsa_factory_init(pjmedia_aud_dev_factory *f); 
    5959static pj_status_t alsa_factory_destroy(pjmedia_aud_dev_factory *f); 
     60static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f); 
    6061static unsigned    alsa_factory_get_dev_count(pjmedia_aud_dev_factory *f); 
    6162static pj_status_t alsa_factory_get_dev_info(pjmedia_aud_dev_factory *f, 
     
    9394    pj_pool_factory             *pf; 
    9495    pj_pool_t                   *pool; 
     96    pj_pool_t                   *base_pool; 
    9597 
    9698    unsigned                     dev_cnt; 
     
    134136    &alsa_factory_get_dev_info, 
    135137    &alsa_factory_default_param, 
    136     &alsa_factory_create_stream 
     138    &alsa_factory_create_stream, 
     139    &alsa_factory_refresh 
    137140}; 
    138141 
     
    146149    &alsa_stream_destroy 
    147150}; 
     151 
     152static void null_alsa_error_handler (const char *file, 
     153                                int line, 
     154                                const char *function, 
     155                                int err, 
     156                                const char *fmt, 
     157                                ...) 
     158{ 
     159    PJ_UNUSED_ARG(file); 
     160    PJ_UNUSED_ARG(line); 
     161    PJ_UNUSED_ARG(function); 
     162    PJ_UNUSED_ARG(err); 
     163    PJ_UNUSED_ARG(fmt); 
     164} 
    148165 
    149166static void alsa_error_handler (const char *file, 
     
    175192 
    176193 
    177 static pj_status_t add_dev (struct alsa_factory *af, int card, int device) 
     194static pj_status_t add_dev (struct alsa_factory *af, const char *dev_name) 
    178195{ 
    179196    pjmedia_aud_dev_info *adi; 
    180     char dev_name[32]; 
    181197    snd_pcm_t* pcm; 
    182198    int pb_result, ca_result; 
     
    187203    adi = &af->devs[af->dev_cnt]; 
    188204 
    189     TRACE_((THIS_FILE, "add_dev (%d, %d): Enter", card, device)); 
    190     sprintf (dev_name, ALSA_DEVICE_NAME, card, device); 
     205    TRACE_((THIS_FILE, "add_dev (%s): Enter", dev_name)); 
    191206 
    192207    /* Try to open the device in playback mode */ 
     
    246261    pj_pool_t *pool; 
    247262 
    248     pool = pj_pool_create(pf, "alsa_aud", 256, 256, NULL); 
     263    pool = pj_pool_create(pf, "alsa_aud_base", 256, 256, NULL); 
    249264    af = PJ_POOL_ZALLOC_T(pool, struct alsa_factory); 
    250265    af->pf = pf; 
    251     af->pool = pool; 
     266    af->base_pool = pool; 
    252267    af->base.op = &alsa_factory_op; 
    253268 
     
    259274static pj_status_t alsa_factory_init(pjmedia_aud_dev_factory *f) 
    260275{ 
     276    pj_status_t status = alsa_factory_refresh(f); 
     277    if (PJ_SUCCESS != status) 
     278        return status; 
     279 
     280    PJ_LOG(4,(THIS_FILE, "ALSA initialized")); 
     281    return PJ_SUCCESS; 
     282} 
     283 
     284 
     285/* API: destroy factory */ 
     286static pj_status_t alsa_factory_destroy(pjmedia_aud_dev_factory *f) 
     287{ 
    261288    struct alsa_factory *af = (struct alsa_factory*)f; 
    262     int card, device; 
     289 
     290    if (af->pool) 
     291        pj_pool_release(af->pool); 
     292 
     293    if (af->base_pool) { 
     294        pj_pool_t *pool = af->base_pool; 
     295        af->base_pool = NULL; 
     296        pj_pool_release(pool); 
     297    } 
     298 
     299    /* Restore handler */ 
     300    snd_lib_error_set_handler(NULL); 
     301 
     302    return PJ_SUCCESS; 
     303} 
     304 
     305 
     306/* API: refresh the device list */ 
     307static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f) 
     308{ 
     309    struct alsa_factory *af = (struct alsa_factory*)f; 
     310    char **hints, **n; 
     311    int err; 
    263312 
    264313    TRACE_((THIS_FILE, "pjmedia_snd_init: Enumerate sound devices")); 
     314 
     315    if (af->pool != NULL) { 
     316        pj_pool_release(af->pool); 
     317        af->pool = NULL; 
     318    } 
     319 
     320    af->pool = pj_pool_create(af->pf, "alsa_aud", 256, 256, NULL); 
     321    af->dev_cnt = 0; 
     322 
    265323    /* Enumerate sound devices */ 
    266     for (card=0; card<MAX_SOUND_CARDS; card++) { 
    267         for (device=0; device<MAX_SOUND_DEVICES_PER_CARD; device++) { 
    268             add_dev(af, card, device); 
     324    err = snd_device_name_hint(-1, "pcm", (void***)&hints); 
     325    if (err != 0) 
     326        return PJMEDIA_EAUD_SYSERR; 
     327 
     328    /* Set a null error handler prior to enumeration to suppress errors */ 
     329    snd_lib_error_set_handler(null_alsa_error_handler); 
     330 
     331    n = hints; 
     332    while (*n != NULL) { 
     333        char *name = snd_device_name_get_hint(*n, "NAME"); 
     334        if (name != NULL && 0 != strcmp("null", name)) { 
     335            add_dev(af, name); 
     336            free(name); 
    269337        } 
     338        n++; 
    270339    } 
    271340 
     
    273342     * error messages about invalid card/device ID. 
    274343     */ 
    275     snd_lib_error_set_handler (alsa_error_handler); 
     344    snd_lib_error_set_handler(alsa_error_handler); 
     345 
     346    err = snd_device_name_free_hint((void**)hints); 
    276347 
    277348    PJ_LOG(4,(THIS_FILE, "ALSA driver found %d devices", af->dev_cnt)); 
    278     return PJ_SUCCESS; 
    279 } 
    280  
    281  
    282 /* API: destroy factory */ 
    283 static pj_status_t alsa_factory_destroy(pjmedia_aud_dev_factory *f) 
    284 { 
    285     struct alsa_factory *af = (struct alsa_factory*)f; 
    286  
    287     if (af->pool) { 
    288         pj_pool_t *pool = af->pool; 
    289         af->pool = NULL; 
    290         pj_pool_release(pool); 
    291     } 
    292  
    293     /* Restore handler */ 
    294     snd_lib_error_set_handler(NULL); 
    295349 
    296350    return PJ_SUCCESS; 
Note: See TracChangeset for help on using the changeset viewer.