Ignore:
Timestamp:
Feb 8, 2006 10:43:39 PM (18 years ago)
Author:
bennylp
Message:

Finished new pjmedia rewrite

File:
1 moved

Legend:

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

    r158 r159  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
    19 #include <pjmedia/mediamgr.h> 
     19#include <pjmedia/endpoint.h> 
     20#include <pjmedia/errno.h> 
     21#include <pjmedia/sdp.h> 
    2022#include <pj/sock.h> 
    2123#include <pj/pool.h> 
    2224#include <pj/string.h> 
    23  
    24 PJ_DECL(pj_status_t) g711_init_factory (pj_codec_factory *factory, pj_pool_t *pool); 
    25 PJ_DECL(pj_status_t) g711_deinit_factory (pj_codec_factory *factory); 
    26  
    27 /** Concrete declaration of media manager. */ 
    28 struct pj_med_mgr_t 
     25#include <pj/assert.h> 
     26#include <pj/os.h> 
     27 
     28 
     29#define THIS_FILE   "endpoint.c" 
     30 
     31static const pj_str_t STR_AUDIO = { "audio", 5}; 
     32static const pj_str_t STR_VIDEO = { "video", 5}; 
     33static const pj_str_t STR_IN = { "IN", 2 }; 
     34static const pj_str_t STR_IP4 = { "IP4", 3}; 
     35static const pj_str_t STR_RTP_AVP = { "RTP/AVP", 7 }; 
     36static const pj_str_t STR_SDP_NAME = { "pjmedia", 7 }; 
     37static const pj_str_t STR_SENDRECV = { "sendrecv", 8 }; 
     38 
     39 
     40PJ_DECL(pj_status_t) g711_init_factory (pjmedia_codec_factory *factory, pj_pool_t *pool); 
     41PJ_DECL(pj_status_t) g711_deinit_factory (pjmedia_codec_factory *factory); 
     42 
     43 
     44/** Concrete declaration of media endpoint. */ 
     45struct pjmedia_endpt 
    2946{ 
    3047    /** Pool. */ 
     
    3552 
    3653    /** Codec manager. */ 
    37     pj_codec_mgr          codec_mgr; 
     54    pjmedia_codec_mgr     codec_mgr; 
    3855}; 
    3956 
    4057/** 
    41  * Initialize and get the instance of media manager. 
    42  */ 
    43 PJ_DEF(pj_med_mgr_t*) pj_med_mgr_create ( pj_pool_factory *pf) 
     58 * Initialize and get the instance of media endpoint. 
     59 */ 
     60PJ_DEF(pj_status_t) pjmedia_endpt_create(pj_pool_factory *pf, 
     61                                         pjmedia_endpt **p_endpt) 
    4462{ 
    4563    pj_pool_t *pool; 
    46     pj_med_mgr_t *mm; 
    47     pj_codec_factory *cf; 
     64    pjmedia_endpt *endpt; 
     65    pjmedia_codec_factory *factory; 
    4866    pj_status_t status; 
    4967 
    50     pool = pj_pool_create(pf, "mediamgr", 512, 512, NULL); 
     68    PJ_ASSERT_RETURN(pf && p_endpt, PJ_EINVAL); 
     69 
     70    pool = pj_pool_create(pf, "med-ept", 512, 512, NULL); 
    5171    if (!pool) 
    52         return NULL; 
    53  
    54     mm = pj_pool_calloc(pool, 1, sizeof(struct pj_med_mgr_t)); 
    55     mm->pool = pool; 
    56     mm->pf = pf; 
     72        return PJ_ENOMEM; 
     73 
     74    endpt = pj_pool_zalloc(pool, sizeof(struct pjmedia_endpt)); 
     75    endpt->pool = pool; 
     76    endpt->pf = pf; 
    5777 
    5878    /* Sound */ 
     
    6080 
    6181    /* Init codec manager. */ 
    62     status = pj_codec_mgr_init(&mm->codec_mgr); 
    63     if (status != 0) { 
     82    status = pjmedia_codec_mgr_init(&endpt->codec_mgr); 
     83    if (status != PJ_SUCCESS) { 
    6484        pj_snd_deinit(); 
    6585        goto on_error; 
     
    6787 
    6888    /* Init and register G.711 codec. */ 
    69     cf = pj_pool_alloc (mm->pool, sizeof(pj_codec_factory)); 
    70  
    71     status = g711_init_factory (cf, mm->pool); 
    72     if (status != 0) { 
     89    factory = pj_pool_alloc (endpt->pool, sizeof(pjmedia_codec_factory)); 
     90 
     91    status = g711_init_factory (factory, endpt->pool); 
     92    if (status != PJ_SUCCESS) { 
    7393        pj_snd_deinit(); 
    74         return NULL; 
    75     } 
    76  
    77     status = pj_codec_mgr_register_factory (&mm->codec_mgr, cf); 
    78     if (status != 0)  
    79         return NULL; 
    80  
    81     return mm; 
     94        goto on_error; 
     95    } 
     96 
     97    status = pjmedia_codec_mgr_register_factory (&endpt->codec_mgr, factory); 
     98    if (status != PJ_SUCCESS)  { 
     99        pj_snd_deinit(); 
     100        goto on_error; 
     101    } 
     102 
     103    *p_endpt = endpt; 
     104    return PJ_SUCCESS; 
    82105 
    83106on_error: 
    84107    pj_pool_release(pool); 
    85     return NULL; 
     108    return status; 
    86109} 
    87110 
     
    89112 * Get the codec manager instance. 
    90113 */ 
    91 PJ_DEF(pj_codec_mgr*) pj_med_mgr_get_codec_mgr (pj_med_mgr_t *mgr) 
    92 { 
    93     return &mgr->codec_mgr; 
    94 } 
    95  
    96 /** 
    97  * Deinitialize media manager. 
    98  */ 
    99 PJ_DEF(pj_status_t) pj_med_mgr_destroy (pj_med_mgr_t *mgr) 
    100 { 
     114PJ_DEF(pjmedia_codec_mgr*) pjmedia_endpt_get_codec_mgr(pjmedia_endpt *endpt) 
     115{ 
     116    return &endpt->codec_mgr; 
     117} 
     118 
     119/** 
     120 * Deinitialize media endpoint. 
     121 */ 
     122PJ_DEF(pj_status_t) pjmedia_endpt_destroy (pjmedia_endpt *endpt) 
     123{ 
     124    PJ_ASSERT_RETURN(endpt, PJ_EINVAL); 
     125 
     126    endpt->pf = NULL; 
     127 
    101128    pj_snd_deinit(); 
    102     pj_pool_release (mgr->pool); 
    103     return 0; 
    104 } 
    105  
    106 /** 
    107  * Get pool factory. 
    108  */ 
    109 PJ_DEF(pj_pool_factory*) pj_med_mgr_get_pool_factory (pj_med_mgr_t *mgr) 
    110 { 
    111     return mgr->pf; 
    112 } 
     129    pj_pool_release (endpt->pool); 
     130 
     131    return PJ_SUCCESS; 
     132} 
     133 
     134/** 
     135 * Create pool. 
     136 */ 
     137PJ_DEF(pj_pool_t*) pjmedia_endpt_create_pool( pjmedia_endpt *endpt, 
     138                                              const char *name, 
     139                                              pj_size_t initial, 
     140                                              pj_size_t increment) 
     141{ 
     142    pj_assert(endpt != NULL); 
     143 
     144    return pj_pool_create(endpt->pf, name, initial, increment, NULL); 
     145} 
     146 
     147/** 
     148 * Create a SDP session description that describes the endpoint 
     149 * capability. 
     150 */ 
     151PJ_DEF(pj_status_t) pjmedia_endpt_create_sdp( pjmedia_endpt *endpt, 
     152                                              pj_pool_t *pool, 
     153                                              unsigned stream_cnt, 
     154                                              const pjmedia_sock_info sock_info[], 
     155                                              pjmedia_sdp_session **p_sdp ) 
     156{ 
     157    pj_time_val tv; 
     158    unsigned i; 
     159    pjmedia_sdp_session *sdp; 
     160    pjmedia_sdp_media *m; 
     161    pjmedia_sdp_attr *attr; 
     162 
     163    PJ_ASSERT_RETURN(endpt && pool && p_sdp && stream_cnt, PJ_EINVAL); 
     164 
     165 
     166    /* Create and initialize basic SDP session */ 
     167    sdp = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_session)); 
     168 
     169    pj_gettimeofday(&tv); 
     170    sdp->origin.user = pj_str("-"); 
     171    sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL; 
     172    sdp->origin.net_type = STR_IN; 
     173    sdp->origin.addr_type = STR_IP4; 
     174    sdp->origin.addr = *pj_gethostname(); 
     175    sdp->name = STR_SDP_NAME; 
     176 
     177    /* Since we only support one media stream at present, put the 
     178     * SDP connection line in the session level. 
     179     */ 
     180    sdp->conn = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_conn)); 
     181    sdp->conn->net_type = STR_IN; 
     182    sdp->conn->addr_type = STR_IP4; 
     183    pj_strdup2(pool, &sdp->conn->addr,  
     184               pj_inet_ntoa(sock_info[0].rtp_addr_name.sin_addr)); 
     185 
     186 
     187    /* SDP time and attributes. */ 
     188    sdp->time.start = sdp->time.stop = 0; 
     189    sdp->attr_count = 0; 
     190 
     191    /* Create media stream 0: */ 
     192 
     193    sdp->media_count = 1; 
     194    m = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_media)); 
     195    sdp->media[0] = m; 
     196 
     197    /* Standard media info: */ 
     198    pj_strdup(pool, &m->desc.media, &STR_AUDIO); 
     199    m->desc.port = pj_ntohs(sock_info[0].rtp_addr_name.sin_port); 
     200    m->desc.port_count = 1; 
     201    pj_strdup (pool, &m->desc.transport, &STR_RTP_AVP); 
     202 
     203    /* Add format and rtpmap for each codec. */ 
     204    m->desc.fmt_count = 0; 
     205    m->attr_count = 0; 
     206 
     207    for (i=0; i<endpt->codec_mgr.codec_cnt; ++i) { 
     208 
     209        pjmedia_codec_info *codec_info = &endpt->codec_mgr.codecs[i]; 
     210        pjmedia_sdp_rtpmap rtpmap; 
     211        pjmedia_sdp_attr *attr; 
     212        pj_str_t *fmt = &m->desc.fmt[m->desc.fmt_count++]; 
     213 
     214        fmt->ptr = pj_pool_alloc(pool, 8); 
     215        fmt->slen = pj_utoa(codec_info->pt, fmt->ptr); 
     216 
     217        rtpmap.pt = *fmt; 
     218        rtpmap.clock_rate = codec_info->sample_rate; 
     219        rtpmap.enc_name = codec_info->encoding_name; 
     220        rtpmap.param.slen = 0; 
     221 
     222        pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr); 
     223        m->attr[m->attr_count++] = attr; 
     224 
     225    } 
     226 
     227    /* Add sendrect attribute. */ 
     228    attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr)); 
     229    attr->name = STR_SENDRECV; 
     230    m->attr[m->attr_count++] = attr; 
     231 
     232 
     233    /* Done */ 
     234    *p_sdp = sdp; 
     235 
     236    return PJ_SUCCESS; 
     237 
     238} 
     239 
Note: See TracChangeset for help on using the changeset viewer.