Ignore:
Timestamp:
Jul 29, 2006 11:14:47 AM (18 years ago)
Author:
bennylp
Message:

Added pjmedia_port_info_init() to fill in pjmedia_port_info structure, avoiding manual initialization and thus improves consistency (and probably reduces size by a tiny bit). This involves modification in quite few places.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia/conference.c

    r582 r633  
    3030#include <pj/string.h> 
    3131 
     32 
    3233/* CONF_DEBUG enables detailed operation of the conference bridge. 
    3334 * Beware that it prints large amounts of logs (several lines per frame). 
     
    5859#define BYTES_PER_SAMPLE    2 
    5960 
     61#define SIGNATURE           PJMEDIA_PORT_SIGNATURE('C', 'O', 'N', 'F') 
     62#define SIGNATURE_PORT      PJMEDIA_PORT_SIGNATURE('C', 'O', 'N', 'P') 
    6063#define NORMAL_LEVEL        128 
    6164#define SLOT_TYPE           unsigned 
     
    197200static pj_status_t get_frame(pjmedia_port *this_port,  
    198201                             pjmedia_frame *frame); 
     202static pj_status_t get_frame_pasv(pjmedia_port *this_port,  
     203                                  pjmedia_frame *frame); 
    199204static pj_status_t destroy_port(pjmedia_port *this_port); 
    200205 
     
    329334} 
    330335 
    331 /* 
    332  * Create port zero for the sound device. 
    333  */ 
    334 static pj_status_t create_sound_port( pj_pool_t *pool, 
    335                                       pjmedia_conf *conf ) 
     336 
     337/* 
     338 * Add passive port. 
     339 */ 
     340static pj_status_t create_pasv_port( pjmedia_conf *conf, 
     341                                     pj_pool_t *pool, 
     342                                     const pj_str_t *name, 
     343                                     pjmedia_port *port, 
     344                                     struct conf_port **p_conf_port) 
    336345{ 
    337346    struct conf_port *conf_port; 
    338     pj_str_t name = { "Master/sound", 12 }; 
    339347    unsigned i; 
    340348    pj_status_t status; 
    341349 
    342  
    343  
    344350    /* Create port */ 
    345     status = create_conf_port(pool, conf, NULL, &name, &conf_port); 
     351    status = create_conf_port(pool, conf, port, name, &conf_port); 
    346352    if (status != PJ_SUCCESS) 
    347         goto on_error; 
    348  
    349     /* Sound device has rx buffers. */ 
     353        return status; 
     354 
     355    /* Passive port has rx buffers. */ 
    350356    for (i=0; i<RX_BUF_COUNT; ++i) { 
    351357        conf_port->snd_buf[i] = pj_pool_zalloc(pool, conf->samples_per_frame * 
    352358                                              sizeof(conf_port->snd_buf[0][0])); 
    353359        if (conf_port->snd_buf[i] == NULL) { 
    354             status = PJ_ENOMEM; 
    355             goto on_error; 
     360            return PJ_ENOMEM; 
    356361        } 
    357362    } 
     
    359364    conf_port->snd_read_pos = 0; 
    360365 
    361  
    362      /* Set to port zero */ 
    363     conf->ports[0] = conf_port; 
    364     conf->port_cnt++; 
     366    *p_conf_port = conf_port; 
     367 
     368    return PJ_SUCCESS; 
     369} 
     370 
     371 
     372/* 
     373 * Create port zero for the sound device. 
     374 */ 
     375static pj_status_t create_sound_port( pj_pool_t *pool, 
     376                                      pjmedia_conf *conf ) 
     377{ 
     378    struct conf_port *conf_port; 
     379    pj_str_t name = { "Master/sound", 12 }; 
     380    pj_status_t status; 
     381 
     382 
     383    status = create_pasv_port(conf, pool, &name, NULL, &conf_port); 
     384    if (status != PJ_SUCCESS) 
     385        return status; 
    365386 
    366387 
     
    395416 
    396417 
     418     /* Add the port to the bridge */ 
     419    conf->ports[0] = conf_port; 
     420    conf->port_cnt++; 
     421 
     422 
    397423    PJ_LOG(5,(THIS_FILE, "Sound device successfully created for port 0")); 
    398424    return PJ_SUCCESS; 
    399  
    400 on_error: 
    401     return status; 
    402  
    403425} 
    404426 
     
    416438{ 
    417439    pjmedia_conf *conf; 
     440    const pj_str_t name = { "Conf", 4 }; 
    418441    pj_status_t status; 
    419442 
     
    443466    PJ_ASSERT_RETURN(conf->master_port, PJ_ENOMEM); 
    444467     
    445     conf->master_port->info.bits_per_sample = bits_per_sample; 
    446     conf->master_port->info.bytes_per_frame = samples_per_frame * 
    447                                               bits_per_sample / 8; 
    448     conf->master_port->info.channel_count = channel_count; 
    449     conf->master_port->info.encoding_name = pj_str("pcm"); 
    450     conf->master_port->info.has_info = 1; 
    451     conf->master_port->info.name = pj_str("sound-dev"); 
    452     conf->master_port->info.need_info = 0; 
    453     conf->master_port->info.pt = 0xFF; 
    454     conf->master_port->info.clock_rate = clock_rate; 
    455     conf->master_port->info.samples_per_frame = samples_per_frame; 
    456     conf->master_port->info.signature = 0; 
    457     conf->master_port->info.type = PJMEDIA_TYPE_AUDIO; 
     468    pjmedia_port_info_init(&conf->master_port->info, &name, SIGNATURE, 
     469                           clock_rate, channel_count, bits_per_sample, 
     470                           samples_per_frame); 
     471 
     472    conf->master_port->port_data.pdata = conf; 
     473    conf->master_port->port_data.ldata = 0; 
    458474 
    459475    conf->master_port->get_frame = &get_frame; 
    460476    conf->master_port->put_frame = &put_frame; 
    461477    conf->master_port->on_destroy = &destroy_port; 
    462  
    463     conf->master_port->user_data = conf; 
    464478 
    465479 
     
    545559static pj_status_t destroy_port(pjmedia_port *this_port) 
    546560{ 
    547     pjmedia_conf *conf = this_port->user_data; 
     561    pjmedia_conf *conf = this_port->port_data.pdata; 
    548562    return pjmedia_conf_destroy(conf); 
    549563} 
     
    630644    return PJ_SUCCESS; 
    631645} 
     646 
     647 
     648/* 
     649 * Add passive port. 
     650 */ 
     651PJ_DEF(pj_status_t) pjmedia_conf_add_passive_port( pjmedia_conf *conf, 
     652                                                   pj_pool_t *pool, 
     653                                                   const pj_str_t *name, 
     654                                                   unsigned clock_rate, 
     655                                                   unsigned channel_count, 
     656                                                   unsigned samples_per_frame, 
     657                                                   unsigned bits_per_sample, 
     658                                                   unsigned options, 
     659                                                   unsigned *p_slot, 
     660                                                   pjmedia_port **p_port ) 
     661{ 
     662    struct conf_port *conf_port; 
     663    pjmedia_port *port; 
     664    unsigned index; 
     665    pj_str_t tmp; 
     666    pj_status_t status; 
     667 
     668    PJ_ASSERT_RETURN(conf && pool, PJ_EINVAL); 
     669 
     670    /* For this version of PJMEDIA, port MUST have the same number of 
     671     * PCM channels. 
     672     */ 
     673    if (channel_count != conf->channel_count) { 
     674        pj_assert(!"Number of channels mismatch"); 
     675        return PJMEDIA_ENCCHANNEL; 
     676    } 
     677 
     678    /* For this version, options must be zero */ 
     679    PJ_ASSERT_RETURN(options == 0, PJ_EINVAL); 
     680    PJ_UNUSED_ARG(options); 
     681 
     682    pj_mutex_lock(conf->mutex); 
     683 
     684    if (conf->port_cnt >= conf->max_ports) { 
     685        pj_assert(!"Too many ports"); 
     686        pj_mutex_unlock(conf->mutex); 
     687        return PJ_ETOOMANY; 
     688    } 
     689 
     690    /* Find empty port in the conference bridge. */ 
     691    for (index=0; index < conf->max_ports; ++index) { 
     692        if (conf->ports[index] == NULL) 
     693            break; 
     694    } 
     695 
     696    pj_assert(index != conf->max_ports); 
     697 
     698    if (name == NULL) { 
     699        name = &tmp; 
     700 
     701        tmp.ptr = pj_pool_alloc(pool, 20); 
     702        tmp.slen = pj_ansi_sprintf(tmp.ptr, "ConfPort#%d", index); 
     703    } 
     704 
     705    /* Create and initialize the media port structure. */ 
     706    port = pj_pool_zalloc(pool, sizeof(pjmedia_port)); 
     707    PJ_ASSERT_RETURN(port, PJ_ENOMEM); 
     708     
     709    pjmedia_port_info_init(&port->info, name, SIGNATURE_PORT, 
     710                           clock_rate, channel_count, bits_per_sample, 
     711                           samples_per_frame); 
     712 
     713    port->port_data.pdata = conf; 
     714    port->port_data.ldata = index; 
     715 
     716    port->get_frame = &get_frame_pasv; 
     717    port->put_frame = &put_frame; 
     718    port->on_destroy = NULL; 
     719 
     720     
     721    /* Create conf port structure. */ 
     722    status = create_pasv_port(conf, pool, name, port, &conf_port); 
     723    if (status != PJ_SUCCESS) { 
     724        pj_mutex_unlock(conf->mutex); 
     725        return status; 
     726    } 
     727 
     728 
     729    /* Put the port. */ 
     730    conf->ports[index] = conf_port; 
     731    conf->port_cnt++; 
     732 
     733    /* Done. */ 
     734    if (p_slot) 
     735        *p_slot = index; 
     736    if (p_port) 
     737        *p_port = port; 
     738 
     739    pj_mutex_unlock(conf->mutex); 
     740 
     741    return PJ_SUCCESS; 
     742} 
     743 
    632744 
    633745 
     
    13631475                             pjmedia_frame *frame) 
    13641476{ 
    1365     pjmedia_conf *conf = this_port->user_data; 
     1477    pjmedia_conf *conf = this_port->port_data.pdata; 
    13661478    unsigned ci, cj, i, j; 
    13671479     
     
    14191531 
    14201532        /* Get frame from this port.  
    1421          * For port zero (sound port), get the frame  from the rx_buffer 
    1422          * instead. 
     1533         * For port zero (sound port) and passive ports, get the frame  from  
     1534         * the rx_buffer instead. 
    14231535         */ 
    1424         if (i==0) { 
     1536        if (conf_port->port == NULL) { 
    14251537            pj_int16_t *snd_buf; 
    14261538 
     
    16161728 
    16171729/* 
     1730 * get_frame() for passive port 
     1731 */ 
     1732static pj_status_t get_frame_pasv(pjmedia_port *this_port,  
     1733                                  pjmedia_frame *frame) 
     1734{ 
     1735    pj_assert(0); 
     1736    return -1; 
     1737} 
     1738 
     1739 
     1740/* 
    16181741 * Recorder callback. 
    16191742 */ 
     
    16211744                             const pjmedia_frame *frame) 
    16221745{ 
    1623     pjmedia_conf *conf = this_port->user_data; 
    1624     struct conf_port *snd_port = conf->ports[0]; 
     1746    pjmedia_conf *conf = this_port->port_data.pdata; 
     1747    struct conf_port *port = conf->ports[this_port->port_data.ldata]; 
    16251748    const pj_int16_t *input = frame->buf; 
    16261749    pj_int16_t *target_snd_buf; 
     
    16321755 
    16331756    /* Skip if this port is muted/disabled. */ 
    1634     if (snd_port->rx_setting != PJMEDIA_PORT_ENABLE) { 
     1757    if (port->rx_setting != PJMEDIA_PORT_ENABLE) { 
    16351758        return PJ_SUCCESS; 
    16361759    } 
    16371760 
    16381761    /* Skip if no port is listening to the microphone */ 
    1639     if (snd_port->listener_cnt == 0) { 
     1762    if (port->listener_cnt == 0) { 
    16401763        return PJ_SUCCESS; 
    16411764    } 
     
    16431766 
    16441767    /* Determine which rx_buffer to fill in */ 
    1645     target_snd_buf = snd_port->snd_buf[snd_port->snd_write_pos]; 
     1768    target_snd_buf = port->snd_buf[port->snd_write_pos]; 
    16461769     
    16471770    /* Copy samples from audio device to target rx_buffer */ 
     
    16491772 
    16501773    /* Switch buffer */ 
    1651     snd_port->snd_write_pos = (snd_port->snd_write_pos+1)%RX_BUF_COUNT; 
    1652  
    1653  
    1654     return PJ_SUCCESS; 
    1655 } 
    1656  
     1774    port->snd_write_pos = (port->snd_write_pos+1)%RX_BUF_COUNT; 
     1775 
     1776 
     1777    return PJ_SUCCESS; 
     1778} 
     1779 
Note: See TracChangeset for help on using the changeset viewer.