From 18e7622287344908ae3ce02164a40336da99d664 Mon Sep 17 00:00:00 2001 From: Liong Sauw Ming Date: Fri, 9 Dec 2011 05:15:39 +0000 Subject: Re #1420: Create a pool for the event manager so subscriber doesn't need to supply its own pool. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3905 74dad513-b988-da41-8d7b-12977e46ad98 --- pjmedia/include/pjmedia/event.h | 2 -- pjmedia/src/pjmedia/event.c | 32 ++++++++++++++++++-------------- pjmedia/src/pjmedia/vid_port.c | 4 ++-- pjmedia/src/pjmedia/vid_stream.c | 2 +- pjmedia/src/test/vid_codec_test.c | 2 +- pjmedia/src/test/vid_dev_test.c | 2 +- pjmedia/src/test/vid_port_test.c | 2 +- pjsip-apps/src/samples/aviplay.c | 2 +- pjsip/src/pjsua-lib/pjsua_vid.c | 8 ++++---- 9 files changed, 29 insertions(+), 27 deletions(-) diff --git a/pjmedia/include/pjmedia/event.h b/pjmedia/include/pjmedia/event.h index ba06625c..84baba9d 100644 --- a/pjmedia/include/pjmedia/event.h +++ b/pjmedia/include/pjmedia/event.h @@ -331,7 +331,6 @@ PJ_DECL(void) pjmedia_event_init(pjmedia_event *event, * events from other publishers. * * @param mgr The event manager. - * @param pool Pool to allocate memory from. * @param cb The callback function to receive the event. * @param user_data The user data to be associated with the callback * function. @@ -340,7 +339,6 @@ PJ_DECL(void) pjmedia_event_init(pjmedia_event *event, * @return PJ_SUCCESS on success or the appropriate error code. */ PJ_DECL(pj_status_t) pjmedia_event_subscribe(pjmedia_event_mgr *mgr, - pj_pool_t *pool, pjmedia_event_cb *cb, void *user_data, void *epub); diff --git a/pjmedia/src/pjmedia/event.c b/pjmedia/src/pjmedia/event.c index 10df2b54..0dfcacb3 100644 --- a/pjmedia/src/pjmedia/event.c +++ b/pjmedia/src/pjmedia/event.c @@ -49,6 +49,7 @@ typedef struct event_queue struct pjmedia_event_mgr { + pj_pool_t *pool; pj_thread_t *thread; /**< worker thread. */ pj_bool_t is_quitting; pj_sem_t *sem; @@ -56,6 +57,7 @@ struct pjmedia_event_mgr event_queue ev_queue; event_queue *pub_ev_queue; /**< publish() event queue. */ esub esub_list; /**< list of subscribers. */ + esub free_esub_list; /**< list of subscribers. */ esub *th_next_sub, /**< worker thread's next sub. */ *pub_next_sub; /**< publish() next sub. */ }; @@ -154,14 +156,18 @@ PJ_DEF(pj_status_t) pjmedia_event_mgr_create(pj_pool_t *pool, pj_status_t status; mgr = PJ_POOL_ZALLOC_T(pool, pjmedia_event_mgr); + mgr->pool = pj_pool_create(pool->factory, "evt mgr", 500, 500, NULL); pj_list_init(&mgr->esub_list); + pj_list_init(&mgr->free_esub_list); if (!(options & PJMEDIA_EVENT_MGR_NO_THREAD)) { - status = pj_sem_create(pool, "ev_sem", 0, MAX_EVENTS + 1, &mgr->sem); + status = pj_sem_create(mgr->pool, "ev_sem", 0, MAX_EVENTS + 1, + &mgr->sem); if (status != PJ_SUCCESS) return status; - status = pj_thread_create(pool, "ev_thread", &event_worker_thread, + status = pj_thread_create(mgr->pool, "ev_thread", + &event_worker_thread, mgr, 0, 0, &mgr->thread); if (status != PJ_SUCCESS) { pjmedia_event_mgr_destroy(mgr); @@ -169,7 +175,7 @@ PJ_DEF(pj_status_t) pjmedia_event_mgr_create(pj_pool_t *pool, } } - status = pj_mutex_create_recursive(pool, "ev_mutex", &mgr->mutex); + status = pj_mutex_create_recursive(mgr->pool, "ev_mutex", &mgr->mutex); if (status != PJ_SUCCESS) { pjmedia_event_mgr_destroy(mgr); return status; @@ -196,8 +202,6 @@ PJ_DEF(void) pjmedia_event_mgr_set_instance(pjmedia_event_mgr *mgr) PJ_DEF(void) pjmedia_event_mgr_destroy(pjmedia_event_mgr *mgr) { - esub *sub; - if (!mgr) mgr = pjmedia_event_mgr_instance(); PJ_ASSERT_ON_FAIL(mgr != NULL, return); @@ -217,12 +221,8 @@ PJ_DEF(void) pjmedia_event_mgr_destroy(pjmedia_event_mgr *mgr) mgr->mutex = NULL; } - sub = mgr->esub_list.next; - while (sub != &mgr->esub_list) { - esub *next = sub->next; - pj_list_erase(sub); - sub = next; - } + if (mgr->pool) + pj_pool_release(mgr->pool); if (event_manager_instance == mgr) event_manager_instance = NULL; @@ -241,14 +241,13 @@ PJ_DEF(void) pjmedia_event_init( pjmedia_event *event, } PJ_DEF(pj_status_t) pjmedia_event_subscribe( pjmedia_event_mgr *mgr, - pj_pool_t *pool, pjmedia_event_cb *cb, void *user_data, void *epub) { esub *sub; - PJ_ASSERT_RETURN(pool && cb, PJ_EINVAL); + PJ_ASSERT_RETURN(cb, PJ_EINVAL); if (!mgr) mgr = pjmedia_event_mgr_instance(); PJ_ASSERT_RETURN(mgr, PJ_EINVAL); @@ -270,7 +269,11 @@ PJ_DEF(pj_status_t) pjmedia_event_subscribe( pjmedia_event_mgr *mgr, sub = next; } - sub = PJ_POOL_ZALLOC_T(pool, esub); + if (mgr->free_esub_list.next != &mgr->free_esub_list) { + sub = mgr->free_esub_list.next; + pj_list_erase(sub); + } else + sub = PJ_POOL_ZALLOC_T(mgr->pool, esub); sub->cb = cb; sub->user_data = user_data; sub->epub = epub; @@ -309,6 +312,7 @@ pjmedia_event_unsubscribe(pjmedia_event_mgr *mgr, if (mgr->pub_next_sub == sub) mgr->pub_next_sub = sub->next; pj_list_erase(sub); + pj_list_push_back(&mgr->free_esub_list, sub); if (user_data && epub) break; } diff --git a/pjmedia/src/pjmedia/vid_port.c b/pjmedia/src/pjmedia/vid_port.c index 1c1cdedf..1e292fb1 100644 --- a/pjmedia/src/pjmedia/vid_port.c +++ b/pjmedia/src/pjmedia/vid_port.c @@ -267,7 +267,7 @@ PJ_DEF(pj_status_t) pjmedia_vid_port_create( pj_pool_t *pool, vparam.fmt.det.vid.fps.num, vparam.fmt.det.vid.fps.denum)); /* Subscribe to device's events */ - pjmedia_event_subscribe(NULL, vp->pool, &vidstream_event_cb, + pjmedia_event_subscribe(NULL, &vidstream_event_cb, vp, vp->strm); if (vp->dir & PJMEDIA_DIR_CAPTURE) { @@ -410,7 +410,7 @@ PJ_DEF(pj_status_t) pjmedia_vid_port_connect(pjmedia_vid_port *vp, vp->client_port = port; /* Subscribe to client port's events */ - pjmedia_event_subscribe(NULL, vp->pool, &client_port_event_cb, vp, + pjmedia_event_subscribe(NULL, &client_port_event_cb, vp, vp->client_port); return PJ_SUCCESS; diff --git a/pjmedia/src/pjmedia/vid_stream.c b/pjmedia/src/pjmedia/vid_stream.c index 8d055770..7b0b1f9d 100644 --- a/pjmedia/src/pjmedia/vid_stream.c +++ b/pjmedia/src/pjmedia/vid_stream.c @@ -1361,7 +1361,7 @@ PJ_DEF(pj_status_t) pjmedia_vid_stream_create( return status; /* Subscribe to codec events */ - pjmedia_event_subscribe(NULL, pool, &stream_event_cb, stream, + pjmedia_event_subscribe(NULL, &stream_event_cb, stream, stream->codec); /* Estimate the maximum frame size */ diff --git a/pjmedia/src/test/vid_codec_test.c b/pjmedia/src/test/vid_codec_test.c index 3df532ab..5d1af672 100644 --- a/pjmedia/src/test/vid_codec_test.c +++ b/pjmedia/src/test/vid_codec_test.c @@ -320,7 +320,7 @@ static int encode_decode_test(pj_pool_t *pool, const char *codec_id, codec_param.dec_fmt.det = codec_param.enc_fmt.det; /* Subscribe to codec events */ - pjmedia_event_subscribe(NULL, pool, &codec_on_event, &codec_port_data, + pjmedia_event_subscribe(NULL, &codec_on_event, &codec_port_data, codec); } diff --git a/pjmedia/src/test/vid_dev_test.c b/pjmedia/src/test/vid_dev_test.c index 5782337b..8f78094a 100644 --- a/pjmedia/src/test/vid_dev_test.c +++ b/pjmedia/src/test/vid_dev_test.c @@ -160,7 +160,7 @@ static int capture_render_loopback(int cap_dev_id, int rend_dev_id, } /* Set event handler */ - pjmedia_event_subscribe(NULL, pool, &vid_event_cb, NULL, renderer); + pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer); /* Connect capture to renderer */ status = pjmedia_vid_port_connect( diff --git a/pjmedia/src/test/vid_port_test.c b/pjmedia/src/test/vid_port_test.c index dd3bf37e..82f0c1bf 100644 --- a/pjmedia/src/test/vid_port_test.c +++ b/pjmedia/src/test/vid_port_test.c @@ -117,7 +117,7 @@ static int capture_render_loopback(pj_bool_t active, } /* Set event handler */ - pjmedia_event_subscribe(NULL, pool, &vid_event_cb, NULL, renderer); + pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer); /* Connect capture to renderer */ status = pjmedia_vid_port_connect( diff --git a/pjsip-apps/src/samples/aviplay.c b/pjsip-apps/src/samples/aviplay.c index bdeab3f9..03fbe800 100644 --- a/pjsip-apps/src/samples/aviplay.c +++ b/pjsip-apps/src/samples/aviplay.c @@ -401,7 +401,7 @@ static int aviplay(pj_pool_t *pool, const char *fname) pjmedia_vid_port_set_cb(renderer, &cb, &avi_port); /* subscribe events */ - pjmedia_event_subscribe(NULL, pool, &avi_event_cb, &avi_port, + pjmedia_event_subscribe(NULL, &avi_event_cb, &avi_port, renderer); if (snd_port) { diff --git a/pjsip/src/pjsua-lib/pjsua_vid.c b/pjsip/src/pjsua-lib/pjsua_vid.c index b8768c00..1ca322fd 100644 --- a/pjsip/src/pjsua-lib/pjsua_vid.c +++ b/pjsip/src/pjsua-lib/pjsua_vid.c @@ -829,7 +829,7 @@ pj_status_t video_channel_update(pjsua_call_media *call_med, #if ENABLE_EVENT /* Register to video events */ - pjmedia_event_subscribe(NULL, w->pool, &call_media_on_event, + pjmedia_event_subscribe(NULL, &call_media_on_event, call_med, w->vp_rend); #endif @@ -899,7 +899,7 @@ pj_status_t video_channel_update(pjsua_call_media *call_med, w = &pjsua_var.win[wid]; #if ENABLE_EVENT - pjmedia_event_subscribe(NULL, w->pool, &call_media_on_event, + pjmedia_event_subscribe(NULL, &call_media_on_event, call_med, w->vp_cap); #endif @@ -1908,7 +1908,7 @@ static pj_status_t call_change_cap_dev(pjsua_call *call, } #if ENABLE_EVENT - pjmedia_event_subscribe(NULL, new_w->pool, &call_media_on_event, + pjmedia_event_subscribe(NULL, &call_media_on_event, call_med, new_w->vp_cap); #endif @@ -1950,7 +1950,7 @@ on_error: #if ENABLE_EVENT /* Resubscribe */ - pjmedia_event_subscribe(NULL, w->pool, &call_media_on_event, + pjmedia_event_subscribe(NULL, &call_media_on_event, call_med, w->vp_cap); #endif -- cgit v1.2.3