Ignore:
Timestamp:
Mar 22, 2006 11:59:11 AM (18 years ago)
Author:
bennylp
Message:

Redesign RTP/RTCP stuffs so that stream does not create thread implicitly. Changed pjmedia_endpt_create() API.

File:
1 edited

Legend:

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

    r321 r350  
    2020#include <pjmedia/errno.h> 
    2121#include <pjmedia/sdp.h> 
     22#include <pj/assert.h> 
     23#include <pj/ioqueue.h> 
     24#include <pj/log.h> 
     25#include <pj/os.h> 
     26#include <pj/pool.h> 
    2227#include <pj/sock.h> 
    23 #include <pj/pool.h> 
    2428#include <pj/string.h> 
    25 #include <pj/assert.h> 
    26 #include <pj/os.h> 
    27 #include <pj/log.h> 
    2829 
    2930 
     
    6667 
    6768 
     69/* Worker thread proc. */ 
     70static int PJ_THREAD_FUNC worker_proc(void*); 
     71 
     72 
     73#define MAX_THREADS     16 
     74 
    6875 
    6976/** Concrete declaration of media endpoint. */ 
     
    7885    /** Codec manager. */ 
    7986    pjmedia_codec_mgr     codec_mgr; 
     87 
     88    /** IOqueue instance. */ 
     89    pj_ioqueue_t         *ioqueue; 
     90 
     91    /** Do we own the ioqueue? */ 
     92    pj_bool_t             own_ioqueue; 
     93 
     94    /** Number of threads. */ 
     95    unsigned              thread_cnt; 
     96 
     97    /** IOqueue polling thread, if any. */ 
     98    pj_thread_t          *thread[MAX_THREADS]; 
     99 
     100    /** To signal polling thread to quit. */ 
     101    pj_bool_t             quit_flag; 
    80102}; 
    81103 
     
    84106 */ 
    85107PJ_DEF(pj_status_t) pjmedia_endpt_create(pj_pool_factory *pf, 
     108                                         pj_ioqueue_t *ioqueue, 
     109                                         unsigned worker_cnt, 
    86110                                         pjmedia_endpt **p_endpt) 
    87111{ 
    88112    pj_pool_t *pool; 
    89113    pjmedia_endpt *endpt; 
     114    unsigned i; 
    90115    pj_status_t status; 
    91116 
     
    105130    endpt->pool = pool; 
    106131    endpt->pf = pf; 
     132    endpt->ioqueue = ioqueue; 
     133    endpt->thread_cnt = worker_cnt; 
    107134 
    108135    /* Sound */ 
     
    111138    /* Init codec manager. */ 
    112139    status = pjmedia_codec_mgr_init(&endpt->codec_mgr); 
    113     if (status != PJ_SUCCESS) { 
    114         pjmedia_snd_deinit(); 
     140    if (status != PJ_SUCCESS) 
    115141        goto on_error; 
    116     } 
    117  
    118     /* Init and register G.711 codec. */ 
    119 #if 0 
    120     // Starting from 0.5.4, codec factory is registered by applications. 
    121     factory = pj_pool_alloc (endpt->pool, sizeof(pjmedia_codec_factory)); 
    122  
    123     status = g711_init_factory (factory, endpt->pool); 
    124     if (status != PJ_SUCCESS) { 
    125         pjmedia_snd_deinit(); 
    126         goto on_error; 
    127     } 
    128  
    129     status = pjmedia_codec_mgr_register_factory (&endpt->codec_mgr, factory); 
    130     if (status != PJ_SUCCESS)  { 
    131         pjmedia_snd_deinit(); 
    132         goto on_error; 
    133     } 
    134 #endif 
     142 
     143    /* Create ioqueue if none is specified. */ 
     144    if (endpt->ioqueue == NULL) { 
     145         
     146        endpt->own_ioqueue = PJ_TRUE; 
     147 
     148        status = pj_ioqueue_create( endpt->pool, PJ_IOQUEUE_MAX_HANDLES, 
     149                                    &endpt->ioqueue); 
     150        if (status != PJ_SUCCESS) 
     151            goto on_error; 
     152 
     153        if (worker_cnt == 0) { 
     154            PJ_LOG(4,(THIS_FILE, "Warning: no worker thread is created in"   
     155                                 "media endpoint for internal ioqueue")); 
     156        } 
     157    } 
     158 
     159    /* Create worker threads if asked. */ 
     160    for (i=0; i<worker_cnt; ++i) { 
     161        status = pj_thread_create( endpt->pool, "media", &worker_proc, 
     162                                   endpt, 0, 0, &endpt->thread[i]); 
     163        if (status != PJ_SUCCESS) 
     164            goto on_error; 
     165    } 
     166 
    135167 
    136168    *p_endpt = endpt; 
     
    138170 
    139171on_error: 
     172 
     173    /* Destroy threads */ 
     174    for (i=0; i<endpt->thread_cnt; ++i) { 
     175        if (endpt->thread[i]) { 
     176            pj_thread_destroy(endpt->thread[i]); 
     177        } 
     178    } 
     179 
     180    /* Destroy internal ioqueue */ 
     181    if (endpt->ioqueue && endpt->own_ioqueue) 
     182        pj_ioqueue_destroy(endpt->ioqueue); 
     183 
     184    pjmedia_snd_deinit(); 
    140185    pj_pool_release(pool); 
    141186    return status; 
     
    155200PJ_DEF(pj_status_t) pjmedia_endpt_destroy (pjmedia_endpt *endpt) 
    156201{ 
     202    unsigned i; 
     203 
    157204    PJ_ASSERT_RETURN(endpt, PJ_EINVAL); 
     205 
     206    endpt->quit_flag = 1; 
     207 
     208    /* Destroy threads */ 
     209    for (i=0; i<endpt->thread_cnt; ++i) { 
     210        if (endpt->thread[i]) { 
     211            pj_thread_join(endpt->thread[i]); 
     212            pj_thread_destroy(endpt->thread[i]); 
     213            endpt->thread[i] = NULL; 
     214        } 
     215    } 
     216 
     217    /* Destroy internal ioqueue */ 
     218    if (endpt->ioqueue && endpt->own_ioqueue) { 
     219        pj_ioqueue_destroy(endpt->ioqueue); 
     220        endpt->ioqueue = NULL; 
     221    } 
    158222 
    159223    endpt->pf = NULL; 
     
    163227 
    164228    return PJ_SUCCESS; 
     229} 
     230 
     231 
     232/** 
     233 * Get the ioqueue instance of the media endpoint. 
     234 */ 
     235PJ_DEF(pj_ioqueue_t*) pjmedia_endpt_get_ioqueue(pjmedia_endpt *endpt) 
     236{ 
     237    PJ_ASSERT_RETURN(endpt, NULL); 
     238    return endpt->ioqueue; 
     239} 
     240 
     241 
     242/** 
     243 * Worker thread proc. 
     244 */ 
     245static int PJ_THREAD_FUNC worker_proc(void *arg) 
     246{ 
     247    pjmedia_endpt *endpt = arg; 
     248 
     249    while (!endpt->quit_flag) { 
     250        pj_time_val timeout = { 0, 500 }; 
     251        pj_ioqueue_poll(endpt->ioqueue, &timeout); 
     252    } 
     253 
     254    return 0; 
    165255} 
    166256 
Note: See TracChangeset for help on using the changeset viewer.